英文:
Java : Xpath variable in xpath evaluate
问题
public Map<String, String> getAllClassesDetails() throws XPathExpressionException {
Map<String, String> data = new HashMap<>();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodesClasses = (NodeList) xpath.evaluate("/Plan/featureMember/node()", document, XPathConstants.NODESET);
for (int i = 0; i < nodesClasses.getLength(); i++) {
String rawXPath = String.format("/Plan/featureMember/%s/node()", nodesClasses.item(i).getNodeName());
NodeList nodesDetails = (NodeList) xpath.evaluate(rawXPath, document, XPathConstants.NODESET);
for (int j = 0; j < nodesDetails.item(i).getChildNodes().getLength(); j++) {
data.put(nodesClasses.item(i).getNodeName(), nodesDetails.item(j).getNodeName());
}
}
return data;
}
<?xml version="1.0" encoding="UTF-8"?>
<Plan>
<featureMember>
<TestA>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestA>
<TestB>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestB>
<TestC>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestC>
</featureMember>
</Plan>
Error:
javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token
How can I specify a variable in xpath.evaluate?
EDIT: Here's my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Plan>
<featureMember>
<TestA>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestA>
<TestB>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestB>
<TestC>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestC>
</featureMember>
</Plan>
英文:
I wouldike to get nodes of nodes in my xml file :
public Map<String, String> getAllClassesDetails() throws XPathExpressionException {
Map<String, String> data = new HashMap<>();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodesClasses = (NodeList) xpath.evaluate("/Plan/featureMember/node()", document, XPathConstants.NODESET);
for (int i = 0; i < nodesClasses.getLength(); i++) {
// System.out.println(nodesClasses.item(i).getNodeName());
String rawXPath = String.format("/Plan/featureMember/%s/node()", nodesClasses.item(i).getNodeName());
NodeList nodesDetails = (NodeList) xpath.evaluate(rawXPath, document, XPathConstants.NODESET);
for (int j = 0; j < nodesDetails.item(i).getChildNodes().getLength(); j++) {
data.put(nodesClasses.item(i).getNodeName(), nodesDetails.item(j).getNodeName());
}
}
return data;
The problem I have an error in this line : NodeList nodesDetails = (NodeList) xpath.evaluate(rawXPath, document, XPathConstants.NODESET);
Error :
javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token
How can I specify variable in xpath.evaluate ?
EDIT : Here my xml file :
<?xml version="1.0" encoding="UTF-8"?>
<Plan>
<featureMember>
<TestA>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestA>
<TestB>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestB>
<TestC>
<Objet></Objet>
<nature>03</nature>
<mastery>5</mastery>
</TestC>
</featureMember>
</Plan>
答案1
得分: 1
我看不出你出现错误的具体原因,但你正在使用的方法效率较低。同时,这种方法可能存在安全隐患,你需要意识到可能发生注入攻击的可能性。
与其为每个nodeClasses条目构建和编译新的XPath表达式,不如使用带有变量的通用表达式:
"/Plan/featureMember/*[local-name=$className]/node()"
使用变量解析器将变量绑定到一个值:
xpath.setXPathVariableResolver(varName -> {
if (varName.getLocalPart().equals("className")) {
return nodesClasses.item(i).getNodeName();
} else {
return "";
}
}
英文:
I can't see exactly why you're hitting the error, but the approach you're using is inefficient. It's also potentially insecure - you need to be aware of the possibility of injection attacks.
Rather than constructing and compiling a new XPath expression for every entry in nodeClasses, use a generic expression with a variable:
"/Plan/featureMember/*[local-name=$className]/node()"
using a variable resolver to bind the variable to a value:
xpath.setXPathVariableResolver(varName -> {
if (varName.getLocalPart().equals("className")) {
return nodesClasses.item(i).getNodeName();
} else {
return "";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论