假设我有以下 XML 文档:
<Devices>
<Scanners>
<atom:link href="http://localhost/111" rel="http://1" />
<atom:link href="http://localhost/222" rel="http://2" />
</Scanners>
<Printers>
<atom:link href="http://localhost/333" rel="http://3" />
<atom:link href="http://localhost/444" rel="http://4" />
</Printers>
<atom:link href="http://localhost/555" rel="http://5" />
</Devices>
使用 REST 放心的 XmlPath
我想阅读全部 <atom:link>
节点 - 实际上是它们的属性列表 - 放入列表中,无论节点位于树中的位置。到目前为止我的代码如下所示:
XmlPath xmlPath = new XmlPath(response);
// This gives me a list of five entries --> OK
List<Node> linkNodes = xmlPath.get("depthFirst().findAll { it.name() == 'link' }");
// This prints five empty lines --> NOT OK
for (Node linkNode : linkNodes) {
System.out.println(linkNode.get("@href"));
}
我在这里缺少什么?
最佳答案
仅执行以下操作就足够了吗?
List<String> links = xmlPath.get("**.findAll { it.name() == 'link' }.@href");
...
关于java - 使用 REST 确保的 XmlPath 查找所有 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28298851/