gpt4 book ai didi

java - 如何使用 xpath 获取对象列表中的所有值?

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:19 24 4
gpt4 key购买 nike

如何使用 xpath 获取所有帐户的名称?以下表达式仅返回第一个帐户名称:

XPathExpression fax = xpath.compile("/accounts/account/name")

<accounts>
<account>
<name>Johndoe1<name>
<account>
<account>
<name>Johndoe2<name>
<account>
</account>

最佳答案

根据本教程:http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html你需要做这样的事情:

XPathExpression fax = xpath.compile("/accounts/account/name")
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}

关于java - 如何使用 xpath 获取对象列表中的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13721048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com