您可以使用XPath查找空文本节点,然后以编程方式将其删除,如下所示:
XPathFactory xpathFactory = XPathFactory.newInstance();// XPath to find empty text nodes.XPathexpression xpathExp = xpathFactory.newXPath().compile( "//text()[normalize-space(.) = '']"); NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);// Remove each empty text node from document.for (int i = 0; i < emptyTextNodes.getLength(); i++) { Node emptyTextNode = emptyTextNodes.item(i); emptyTextNode.getParentNode().removeChild(emptyTextNode);}
如果您想比XSL模板更容易控制节点删除,则此方法可能很有用。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)