1

I have a XML object and Im converting into Doc and trying to fetch one of the tag using Xpath, it returns blank. Please find the code.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns:BB_SDF_Response
        xmlns:ns="<url>">
        <ns:BBServiceResponse
            xmlns="<url>">
            <ns1:BBHeader
                xmlns:ns1="<url>">
                <ns1:rsHeader>
                    <ns1:status>
                        <ns1:overallStatus>E</ns1:overallStatus>
                        <ns1:bbMessages>
                            <ns1:formattedMessage>Location does not exist</ns1:formattedMessage>
                        </ns1:bbMessages>
                    </ns1:status>
                </ns1:rsHeader>
            </ns1:BBHeader>
        </ns:BBServiceResponse>
    </ns:BB_SDF_Response>
</soapenv:Body>
</soapenv:Envelope>

My Java code is

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
                        dbf.setNamespaceAware(true); 
DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 
org.w3c.dom.Document doc = docBuilder.parse(new InputSource(new StringReader(soapResponse)));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();  
logger.info(xpath.evaluate("/Envelope/Body/BB_SDF_Response/BBServiceResponse/BBHeader/rsHeader/status/bbMessages/formattedMessage/",doc, XPathConstants.STRING));

I want to fetch formattedMessage tag value.

3
  • Might be you're not passing the namespace. I'm not Java developer but got similar problem in C#. Commented Oct 15, 2017 at 9:15
  • But namespace matter? Commented Oct 15, 2017 at 9:16
  • in .Net, if the xml has namespace you get null values for xpath unless namespace is specified in your code. Perhaps setting dbf.setNamespaceAware(false) could ignore namespace, but I don't know. See this question stackoverflow.com/questions/6390339/… Commented Oct 15, 2017 at 9:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.