【发布时间】:2021-12-23 15:32:49
【问题描述】:
我有如下的xml
<results>
<result>
<location>/tmp/path1</location>
<name>name1</name>
</result>
<result>
<location>/tmp/path2</location>
<name>name2</name>
</result>
</results>
我想遍历所有结果对象,并从每个对象中使用 location 和 name。
我正在使用以下代码获取结果对象列表
XPathExpression expression = XPathFactory.newInstance().newXPath().compile("/results/result");
NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
// ??
}
如何从节点获取location 和name 的值?
【问题讨论】:
标签: java xml xpath child-nodes