J
JS
I have this XML file:
<root>
<lines>
<line order="0">
<content id="A" value="0_A"/>
<content id="B" value="0_B"/>
<content id="C" value="0_C"/>
<content id="D" value="0_D"/>
<content id="E" value="0_E"/>
</line>
<line order="1">
<content id="A" value="1_A"/>
<content id="B" value="1_B"/>
<content id="C" value="1_C"/>
<content id="D" value="1_D"/>
<content id="E" value="1_E"/>
</line>
</lines>
</root>
From Java I am trying to print values for all elements where ID = "A":
public void test(){
javax.xml.xpath.XPathFactory factory =
javax.xml.xpath.XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = factory.newXPath();
javax.xml.xpath.XPathExpression expression;
String result;
try {
expression = xpath.compile("//content[@id =\"A\"]/@value");
URL url = this.getClass().getClassLoader().getResource("test.xml");
String path = url.toURI().getPath();
result = expression.evaluate(new org.xml.sax.InputSource(path));
System.out.println("result is = " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
But only:
0_A
is printed. Does "//" not mean that all 'content' elements are chosen?
<root>
<lines>
<line order="0">
<content id="A" value="0_A"/>
<content id="B" value="0_B"/>
<content id="C" value="0_C"/>
<content id="D" value="0_D"/>
<content id="E" value="0_E"/>
</line>
<line order="1">
<content id="A" value="1_A"/>
<content id="B" value="1_B"/>
<content id="C" value="1_C"/>
<content id="D" value="1_D"/>
<content id="E" value="1_E"/>
</line>
</lines>
</root>
From Java I am trying to print values for all elements where ID = "A":
public void test(){
javax.xml.xpath.XPathFactory factory =
javax.xml.xpath.XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = factory.newXPath();
javax.xml.xpath.XPathExpression expression;
String result;
try {
expression = xpath.compile("//content[@id =\"A\"]/@value");
URL url = this.getClass().getClassLoader().getResource("test.xml");
String path = url.toURI().getPath();
result = expression.evaluate(new org.xml.sax.InputSource(path));
System.out.println("result is = " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
But only:
0_A
is printed. Does "//" not mean that all 'content' elements are chosen?