[英]XPath evaluation in for loop always returns value for node from first Iteration
I am trying to extract values from a node using a method which is invoked within a for loop. 我试图使用在for循环中调用的方法从节点中提取值。
Every time the Method is invoked, the xpath.evaluate only evaluates the original node that was first passed into the method. 每次调用Method时,xpath.evaluate仅计算首次传递给方法的原始节点。
-Sample Node - 示例节点
<doc>
<str name="subcategory">xyzabc</str>
<str name="type">QAs</str>
<str name="id">1234</str>
</doc>
-- for loop where invoked - 用于调用的循环
ArrayList<Node> responseNodeList = parseResponse(*document as string*);
for(int k = 0;k<responseNodeList.size();k++){
Result resultNode = new Result();
resultNode.setUrl(new URL(getAttributeValue(responseNodeList.get(k),resultUrl)));
resultNode.setId(new String(getAttributeValue(responseNodeList.get(k),resultId)));
}
-- Method where i extract the value - 我提取值的方法
private String getAttributeValue(final Node responseNodeUrl, String attribute) throws SearchException {
try {
Node evalNode = null;
evalNode = responseNodeUrl;
try {
nodelisttostr(responseNodeUrl); // This is what i am printing out the node.. where we dont have a problem
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr = null;
expr = xPath.compile("/doc/str[@name='"+attribute+"']");
String result ;
result=(String) expr.evaluate(evalNode,XPathConstants.STRING);
xPath.reset();
System.out.println("++++++++"+result); //But when i print this out, it always prints the same value
return result;
} catch (XPathExpressionException e) {
log.error("Error in SearchEngine:getAttributeValue:: "+e.getMessage());
throw new SearchException(e);
}
}
Solved.. 解决了..
The XpathExpression was always picking up the first node passed to the method and for some reason it seemed like it was cached, I changed my evaluation string to look like expr = xPath.compile(".//str[@name='"+attribute+"']");
XpathExpression总是拿起传递给方法的第一个节点,由于某种原因它似乎被缓存了,我把我的评估字符串改为看起来像
expr = xPath.compile(".//str[@name='"+attribute+"']");
and then the evaluate method now pics up the current document. 然后评估方法现在编写当前文档。
Thank you All. 谢谢你们。
If on your XML above, you use: 如果在上面的XML上,您使用:
//doc/str/@name
You will get: 你会得到:
Attribute='name="subcategory"'
Attribute='name="type"'
Attribute='name="id"'
If you use contains
in the XPath expression on your XML above ie 如果在XML上面的XPath表达式中使用
contains
,即
//doc/str/@name[contains(.,'e')]
then you will get: 然后你会得到:
Attribute='name="subcategory"'
Attribute='name="type"'
But I think you want to just want to do the following with your code: 但我想你只想用你的代码做以下事情:
change this: 改变这个:
expr = xPath.compile("/doc/str[@name='"+attribute+"']");
to this ie add an extra /
: 对此,即添加额外的
/
:
expr = xPath.compile("//doc/str[@name='"+attribute+"']");
To evaluate all nodes instead of just the first. 评估所有节点而不是第一个节点。
I hope this helps! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.