Link to home
Start Free TrialLog in
Avatar of narcist527
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
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

There are multiple ways to do it.

XPath is the key here.

Are you using any XML library or are you constrained to using javax.xml API?
Avatar of narcist527
narcist527

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 .
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).evaluate(docu,XPathConstants.NODESET);
Avatar of CEHJ
http://technojeeves.com/index.php/aliasjava1/26-xpath-tester

The expression given to you works fine with that (after correcting your xml)