tags:

views:

70

answers:

1

I have the following soap message:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;&lt;soap:Header&gt;&lt;wsse:Security env:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;&lt;wsse:Username&gt;test&lt;/wsse:Username&gt;&lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;11111111&lt;/wsse:Password&gt;&lt;/wsse:UsernameToken&gt;&lt;/wsse:Security&gt;&lt;/soap:Header&gt;
    <soap:Body xmlns:ns1="http://www.acme.co.za"&gt;
        <ns1:pingMeCallback>
            <ns1:message>abc</ns1:message>
        </ns1:pingMeCallback>
    </soap:Body>
</soap:Envelope>

I need to use xpath to read in the message value. My code is as follows:

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

  InputSource data = new InputSource(in);
  Node doc = builder.parse("ping.xml");

  // set up a document purely to hold the namespace mappings
  DOMImplementation impl = builder.getDOMImplementation();
  Document namespaceHolder = impl.createDocument("http://www.acme.co.za/","f:namespaceMapping", null);
  Element root = namespaceHolder.getDocumentElement();

  root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:SOAP","http://schemas.xmlsoap.org/soap/envelope/");
  root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns1","http://schemas.xmlsoap.org/soap/envelope/");

  NodeList results = XPathAPI.selectNodeList(doc,"/SOAP:Envelope/SOAP:Body/ns1:pingMeCallback/ns1:message",root);

  for (int i = 0; i < results.getLength(); i++) {
   Node result = results.item(i);
   XObject value = XPathAPI.eval(result, "string()");
   System.out.println(value.str());
  }

My code prints out nothing. Can anybody find out what I am doing wroing?

A: 

Haven't gone through the rest of the code, but:

root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns1","http://schemas.xmlsoap.org/soap/envelope/");

doesn't look like the right namespace.

bobince
I have changed it to: root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns1","hhttp://www.acme.co.za"); Still no luck
Still doesn't quite match. Double `h`; also check trailing vs. non-trailing `/`. Namespaces are strings that must match exactly.
bobince