Xpath seems to not work. I've already tried a few things, but nothing seems to work. What am I doing wrong?
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(result)));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//cm:URL/@value");
String msg = expr.evaluate(doc, XPathConstants.STRING).toString();
logger.debug(msg);
The XML I have looks like this:
<ItemXML xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:ns2="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema">
<DOCUMENTS SCA_DATE="#" SCA_NR="#" cm:PID="#" xmlns:cm="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema" xmlns:ns1="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cm:properties type="#">
<cm:lastChangeUserid value="#"/>
<cm:lastChangeTime value="#"/>
<cm:createUserid value="#"/>
<cm:createTime value="#"/>
<cm:semanticType value="#"/>
<cm:ACL name="#"/>
<cm:lastOperation name="#" value="#"/>
</cm:properties>
<ns1:CONTRACTS AM="#"/>
<ns1:BASE cm:PID="#" cm:partNumber="#">
<cm:properties type="item" xsi:type="#">
<cm:lastChangeUserid value="#"/>
<cm:lastChangeTime value="#"/>
<cm:createUserid value="#"/>
<cm:createTime value="#"/>
<cm:semanticType value="#"/>
<cm:ACL name="#"/>
<cm:lastOperation name="#" value="#"/>
</cm:properties>
<cm:resourceObject MIMEType="application/pdf" RMName="#" SMSCollName="#" externalObjectName="" originalFileName="#" resourceFlag="#" resourceName="" size="#">
<cm:URL value="https://testurl.com"/>
</cm:resourceObject>
</ns1:BASE>
</DOCUMENTS>
</ItemXML>
I want the value of cm:URL --> https://testurl.com saved as String. Important: Xpath should find the value regardless of the xml structure.