First I noticed that your XML
is not valid.
Assume that your XML is as follows.
XML:
<Root>
<SubOne>
<book author="george"/>
<indiv name="abc"/>
<indiv name="khh"/>
<indiv name="ioo"/>
</SubOne>
<SubTwo>
<book author="hamle"/>
<indiv name="kok"/>
<indiv name="ppp"/>
</SubTwo>
</Root>
XPATH:
For @author='george'
, use this XPATH:
//*[book[@author='george']]/indiv
For @author='hamle'
, use this XPATH:
//*[book[@author='hamle']]/indiv
For both XPATH, to print the name use @name
JAVA:
Using Java, you need to implement this way for both xpath's
For @author='george'
,
String xpath = "//*[book[@author='george']]/indiv";
NodeList nl = (NodeList) xpath.evaluate(xpath, xml, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getAttributes().getNamedItem("name").getNodeValue());
}
For @author='hamle'
,
String xpath = "//*[book[@author='hamle']]/indiv";
NodeList nl = (NodeList) xpath.evaluate(xpath, xml, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getAttributes().getNamedItem("name").getNodeValue());
}