Node.getTextContent() is there a way to get text content of the current node, not the descendant's text

后端 未结 4 762
眼角桃花
眼角桃花 2021-02-02 14:53

Node.getTextContent() returns the text content of the current node and its descendants.

is there a way to get text content of the current node, not the descendant\'s tex

4条回答
  •  悲哀的现实
    2021-02-02 15:28

    I do this with Java 8 streams and a helper class:

    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 list(NodeList nodeList)
        {
            List list = new ArrayList<>();
            for(int i=0;i

    And then

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

提交回复
热议问题