Reputation: 13649
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?
Upvotes: 1
Views: 787
Reputation: 5758
try :
NodeList nl = (NodeList)xPath.evaluate("/CU_DATA/CU/CON/LANGUAGES//LANGUAGE", document, XPathConstants.NODESET);
or
NodeList nl = (NodeList)xPath.evaluate("/CU_DATA/CU/CON/LANGUAGES//*", document, XPathConstants.NODESET);
/
is used to separate the nodes where as //
returns all the nodes recursively
Upvotes: 0
Reputation: 1500893
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.
Upvotes: 3