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.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Yakhoob
  • 559
  • 1
  • 12
  • 32
  • Might be you're not passing the namespace. I'm not Java developer but got similar problem in C#. – derloopkat Oct 15 '17 at 09:15
  • But namespace matter? – Yakhoob Oct 15 '17 at 09: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 https://stackoverflow.com/questions/6390339/how-to-query-xml-using-namespaces-in-java-with-xpath – derloopkat Oct 15 '17 at 09:29

0 Answers0