【发布时间】:2022-08-16 04:21:19
【问题描述】:
我正在尝试选择一个节点,但没有选择它的任何子节点。
示例输入:
<Header attr1=\"Hello\">
<child1> hello </child1>
<child2>world</child2>
</Header>
预期输出:<Header attr1=\"Hello\"> </Header>
代码:
Document xmlDoc = saxBuilder.build(inputStream);
Xpath x = XPath.newInstance(\"/Header\");
eleMyElement = x.selectSingleNode(xmlDoc);
XMLOutputter output = new XMLOutputter();
output.outputString(eleMyElement) --> this is the output
我尝试使用 /Header 作为 XPath,它为我提供了标题以及子节点。
-
XPath 选择文档中的内容。您的文档中没有没有子元素的
Header元素。通常,XSLT 将用于从输入文档的部分构造输出文档。通过可能的更多努力,您可以使用另一个 API(例如 JDOM 提供的 API)来实现相同的结果。不过,它需要的不仅仅是 XPath。