0

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.

PinkBanter
  • 1,686
  • 5
  • 17
  • 38
Darim
  • 3
  • 2
  • Possible duplicate of [XPath with namespace in Java](https://stackoverflow.com/questions/13702637/xpath-with-namespace-in-java) ; I've been able to confirm that's your problem, see https://ideone.com/bpy2eJ#stdout (no prefix, works) vs https://ideone.com/V0HVC3#stdout (prefix, doesn't work) – Aaron Feb 05 '19 at 14:13
  • Thank you @Aaron! That's it! – Darim Feb 05 '19 at 14:18

3 Answers3

0

Without using XPath you can do:

NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");
System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue());
Nicola Ambrosetti
  • 2,567
  • 3
  • 22
  • 38
0

There is issue in following line :-

Document doc = builder.parse(new InputSource(new StringReader(result)));

i tried to print the document, it is giving me output as [#document: null], can you first try to print the document in console and let me know if it is not null.

0

as suggested by @bangnab use his solution, i tried its working

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XpathTester {

    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        File inputFile = new File("C:\\Users\\Arvind.Carpenter\\Desktop\\input.txt");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputFile);
        doc.getDocumentElement().normalize();
        NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");

        //you can iterate over this node list and get all the URL i am printing first one

        System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue()); 
    }

}