I need to join two XPath queries that refer to different nodes to get all possible combinations of countries and their statuses from the xml.
Is there any method to extract several attributes from different elements not separately?
What have I tried?
A note: I have * before [@Country] and [@Status] in all my queries so the code below with * works. For some reason * is not displayed correctly within the text below.
- Just for a check I separated the code below to two different XPath "//*[@Country]" and "//[@Status]" and duplicating code => the correct data is exported for each query so I can get two lists of data for countries (7000 entries) and statuses (7000 entries) however I need to extract this data using one request.
- Changing "//[@Country] | //[@Status]") to ("//[@Country] and //[@Status]") does not work -> XPathExpressionException appears: Can not convert #BOOLEAN to a NodeList!
If try using the code below, countryStatusNodes.getLength() has 14000 while it should have had 7000 entries.
So it seems that the mistake is still in ("//[@Country] | //[@Status]".
XPathExpression countryStatusExpr = XPathFactory.newInstance().newXPath().compile("//*[@Country] | //*[@Status]");
Object countryStatusResult = countryStatusExpr.evaluate(document, XPathConstants.NODESET);
NodeList countryStatusNodes = (NodeList) countryStatusResult;
for (int i = 0; i < countryStatusNodes.getLength(); i++) {
NamedNodeMap attr = countryStatusNodes.item(i).getAttributes();
Node country = attr.getNamedItem("Country");
Node status = attr.getNamedItem("Status");
nodes.add(country.getNodeValue() + "," + status.getNodeValue());
}
xml:
<Document>
<Info>
<Name FullName=".." ShortName="..." />
</Info>
<Location>
<Adress Code=".." Country=".." FullAdress="..." IncorporationDate="..." />
</Location>
<Registration>
<License Status=".." LicenseDate=".." />
</Registration>
</Document>