Node.getTextContent()有一种方法可以获取当前节点的文本内容,而不是后代的文本

时间:2012-08-30 06:54:24

标签: java xml dom

Node.getTextContent()返回当前节点及其后代的文本内容。

有没有办法获取当前节点的文本内容,而不是后代的文本。

示例

<paragraph>
    <link>XML</link>
    is a 
    <strong>browser based XML editor</strong>
    editor allows users to edit XML data in an intuitive word processor.
</paragraph>

预期产出

paragraph = is a editor allows users to edit XML data in an intuitive word processor.
link = XML
strong = browser based XML editor

我试过下面的代码

String str =            "<paragraph>"+
                            "<link>XML</link>"+
                            " is a "+ 
                            "<strong>browser based XML editor</strong>"+
                            "editor allows users to edit XML data in an intuitive word processor."+
                        "</paragraph>";

        org.w3c.dom.Document domDoc = null;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;

        try {
            docBuilder = docFactory.newDocumentBuilder();
            ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
            domDoc = docBuilder.parse(bis);         
        } catch (ParserConfigurationException e1) {         
            e1.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }       

        DocumentTraversal traversal = (DocumentTraversal) domDoc;
        NodeIterator iterator = traversal.createNodeIterator(
                domDoc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

        for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {           
            String tagname = ((Element) n).getTagName();
            System.out.println(tagname + "=" + ((Element)n).getTextContent());
        }

但它提供了像这样的输出

paragraph=XML is a browser based XML editoreditor allows users to edit XML data in an intuitive word processor.
link=XML
strong=browser based XML editor

请注意元素包含链接标记的文字,我不想要。 请提出一些想法?

4 个答案:

答案 0 :(得分:13)

您想要的是过滤节点<paragraph>的子节点,只保留节点类型为Node.TEXT_NODE的子节点。

这是一个返回所需内容的方法示例

public static String getFirstLevelTextContent(Node node) {
    NodeList list = node.getChildNodes();
    StringBuilder textContent = new StringBuilder();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            textContent.append(child.getTextContent());
    }
    return textContent.toString();
}

在你的例子中,它意味着:

String str = "<paragraph>" + //
        "<link>XML</link>" + //
        " is a " + //
        "<strong>browser based XML editor</strong>" + //
        "editor allows users to edit XML data in an intuitive word processor." + //
        "</paragraph>";
Document domDoc = null;
try {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
    domDoc = docBuilder.parse(bis);
} catch (Exception e) {
    e.printStackTrace();
}
DocumentTraversal traversal = (DocumentTraversal) domDoc;
NodeIterator iterator = traversal.createNodeIterator(domDoc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
    String tagname = ((Element) n).getTagName();
    System.out.println(tagname + "=" + getFirstLevelTextContent(n));
}

输出:

paragraph= is a editor allows users to edit XML data in an intuitive word processor.
link=XML
strong=browser based XML editor

它的作用是迭代一个Node的所有子节点,只保留TEXT(从而排除注释,节点等)并累积它们各自的文本内容。

NodeElement中没有直接方法只能获取第一级的文字内容。

答案 1 :(得分:3)

如果您将最后一个for循环更改为下一个循环,则其行为符合您的要求

for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {           
    String tagname = ((Element) n).getTagName();
    StringBuilder content = new StringBuilder();
    NodeList children = n.getChildNodes();
    for(int i=0; i<children.getLength(); i++) {
        Node child = children.item(i);
        if(child.getNodeName().equals("#text"))
            content.append(child.getTextContent());
    }
    System.out.println(tagname + "=" + content);
}

答案 2 :(得分:2)

我使用Java 8流和帮助程序类执行此操作:

import java.util.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NodeLists
{
    /** converts a NodeList to java.util.List of Node */
    static List<Node> list(NodeList nodeList)
    {
        List<Node> list = new ArrayList<>();
        for(int i=0;i<nodeList.getLength();i++) {list.add(nodeList.item(i));}
        return list;
    }
}

然后

 NodeLists.list(node)
.stream()
.filter(node->node.getNodeType()==Node.TEXT_NODE)
 .map(Node::getTextContent)
 .reduce("",(s,t)->s+t);

答案 3 :(得分:1)

隐含地没有实际节点文本的任何功能,但只需一个简单的技巧即可。询问node.getTextContent()是否包含&#34; \ n&#34;,如果是这种情况,则实际节点不会有任何文本。

希望得到这个帮助。