有没有办法在DOM中的HTML标签之间逐字提取文本?

有没有办法在DOM中的HTML标签之间逐字提取文本?

问题描述:

例如,对于HTML文档:

For example, for an HTML document:

<html>
<body>
  <p>Hello <b>world</b></p>
</body>
</html>

我知道我可以使用 getTextContent()要获得Hello world,但是有没有办法获取Hello< b> world< / b>

I know I can use getTextContent() to get "Hello world", but is there a way to get "Hello <b>world</b>"?

在Java中,您可以应用XSLT转换: https://docs.oracle.com/javase/tutorial/jaxp/xslt/writingDom.html

In Java, you can apply a XSLT transformation: https://docs.oracle.com/javase/tutorial/jaxp/xslt/writingDom.html

您需要的是检索元素子树的XML内容。解决这个问题的SO有几个问题:
如何在java中输出org.w3c.dom.Element为字符串格式?

What you need is to retrieve the XML content of the element subtree. There are several questions in SO addressing this issue: How to I output org.w3c.dom.Element to string format in java?

Java / DOM:获取节点的XML内容

获取节点的内部XML作为Java DOM中的String