I have the following XML structure:
...
...
<CON>
<LANGUAGES>
<LANGUAGE>Deutsch</LANGUAGE>
<LANGUAGE>English</LANGUAGE>
</LANGUAGES>
<CON>
...
...
Using my code below, I'm trying to retrieve the languages but when I try to print the length of the node list, it only returns 1.
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList)xPath.evaluate("/CU_DATA/CU/CON/LANGUAGES", document, XPathConstants.NODESET);
System.out.println(nl.getLength());
// Output: 1
How can I get the two languages?
How can I get the two languages?
By asking for the LANGUAGE
elements instead of LANGUAGES
- after all, there is only one LANGUAGES
element. So something like this:
NodeList nl = (NodeList)xPath.evaluate("/CU_DATA/CU/CON/LANGUAGES/LANGUAGE",
document, XPathConstants.NODESET);
Alternatively, find the LANGUAGES
element as you're currently doing, and then just fetch all the child nodes.
See more on this question at Stackoverflow