Java XPath Compile can't query tag with unique namespace

Solution 1:

Your current XPath expression only matches Info and property elements with no namespace uri.

You must add the urn:...Service2-namespace to you NamespaceContext inner class:

public String getNamespaceURI(String prefix) {
    if ("cam".equals(prefix))
       // like abvce
    else if ("s2".equals(prefix))
       return "urn:xxx:1.0:xxx.core.xxx.Service2";
    else
       return XMLConstants.NULL_NS_URI;
}

and qualify the Info and property step in your XPath accordingly, using prefix s2:

xpath.compile("/cam:message/cam:response/soapenv:Envelope/soapenv:Header/s2:Info/s2:property[@name='response']/text()");

Beyond that there are errors in your code and the XML file:

a) You make the DocumentBuilderFactory namespace-aware after you have created the DocumentBuilder. Write instead:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();

b) Your XML uses the prefix cam which is not properly declared in the root element. Write instead:

<cam:message xmlns:cam="urn:xxx:1.0:logging">
    ....