您想要的是过滤节点的子代,
<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=XMLstrong=browser based XML editor
它的作用是在节点的所有子节点上进行迭代,仅保留TEXT(因此不包括注释,节点等)并累积其各自的文本内容。
有中没有直接的方法
Node或
Element得到的只有在第一级别的文本内容。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)