gpt4 book ai didi

java - java中的XPath不接受表达式中的变量

转载 作者:行者123 更新时间:2023-12-01 16:03:32 25 4
gpt4 key购买 nike

有人可以解释一下为什么吗
xPath.evaluate("/MEMBER_LIST/MEMBER[1]/ADDRESS", nodeMemberList, XPathConstants.STRING)返回我正在寻找的值以及原因 xPath.evaluate("/MEMBER_LIST/MEMBER["+ i + "]/ADDRESS", nodeMemberList, XPathConstants.STRING)返回空字符串?我必须在 for 循环中执行这些操作,因此这里“i”是代表当前条目的 int。

最佳答案

你的for循环是从1开始的吗?表达式 /MEMBER_LIST/MEMBER[0] 不是有效的 XPath 表达式,因为 XPath 索引从 1 开始。此外,访问超过节点总数的索引也是无效的。例如,当只有 3 个 MEMBER 元素时执行 /MEMBER_LIST/MEMBER[5]

您还可以使用 XPathConstants.NODESET 常量。这将返回与给定表达式匹配的所有元素的列表。然后您可以迭代该列表。

NodeList nodeList = (NodeList)xPath.evaluate("/MEMBER_LIST/MEMBER/ADDRESS", nodeMemberList, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i){
Node node = nodeList.item(i);
String address = node.getTextContent();
}

关于java - java中的XPath不接受表达式中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3199053/

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