I have an XML
of the form:
<?xml version="1.0" encoding="UTF-8"?>
<semseg:Envelope xmlns:semseg="http://a-random-URL" xmlns="http://another-random-URL">
<semseg:subject>Subject</semseg:subject>
<semseg:Sender>
<semseg:name>Me</semseg:name>
</semseg:Sender>
<Triangle>
<Triangle time='2017-11-29'>
<Triangle key='a' value='b'/>
<Triangle key='c' value='d'/>
<Triangle key='e' value='f'/>
<Triangle key='g' value='h'/>
</Triangle>
</Triangle>
</semseg:Envelope>
And I am trying to retrieve the element <Triangle>
(not <Triangle time='2017-11-29'>
- element names are a bit repetitive in this XML) using XPath
. Part of the code is the following:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse("file.xml");
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xpr = xPath.compile("/semseg:Envelope/Triangle");
NodeList nodes = (NodeList)xpr.evaluate(doc, XPathConstants.NODESET);
I have tried many possible combinations for the XPath
without any luck unfortunately since no elements are selected. Nevertheless, testing the same XPath
with this online XPath
checker and the same XML
file yields exactly the results I am looking for. It evens works for attribute retrieval using XPaths like
/semseg:Envelope/Triangle/Triangle/@time
Seems like there is a problem with the namespace prefixes. Parsing XML
s without any namespace prefixes works just fine with XPath
.