narcist527
asked on
retrieving element values from xml in java
I have multiple tags <a>value</a>. in different nodes. like
<root>
<x>
<a>value</a>
</x>
<y>
<a>value</a>
</x>
<x>
<xcv>
<a>value</a>
</xcv>
</y>
<z>
<a>value</a>
</z>
</root>
i want to retrieve all 'a' values.
appreciate any help
<root>
<x>
<a>value</a>
</x>
<y>
<a>value</a>
</x>
<x>
<xcv>
<a>value</a>
</xcv>
</y>
<z>
<a>value</a>
</z>
</root>
i want to retrieve all 'a' values.
appreciate any help
ASKER
@gurpsbassi
we can not use xpath here since it is not specific. it can be any where. I am using DOM parser to retrieve .
we can not use xpath here since it is not specific. it can be any where. I am using DOM parser to retrieve .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
below one is my code and it is returning 0 elements . any help where I am making mistake? I am using the xml file .
XPath xPath = XPathFactory.newInstance() .newXPath( );
InputSource docu = new InputSource(new InputStreamReader(new FileInputStream(new File(file))));
String xpression = "//a";
NodeList nodes = (NodeList) xPath.compile(xpression).e valuate(do cu,XPathCo nstants.NO DESET);
XPath xPath = XPathFactory.newInstance()
InputSource docu = new InputSource(new InputStreamReader(new FileInputStream(new File(file))));
String xpression = "//a";
NodeList nodes = (NodeList) xPath.compile(xpression).e
http://technojeeves.com/index.php/aliasjava1/26-xpath-tester
The expression given to you works fine with that (after correcting your xml)
The expression given to you works fine with that (after correcting your xml)
XPath is the key here.
Are you using any XML library or are you constrained to using javax.xml API?