jigsaw
jigsaw

Reputation: 11

how to parse data from SOAP reponse message in java

I am using Dom4j to parse SOAP message. I had a problem:
Node node = document.selectSingleNode( "/*/soapenv:Envelope/soapenv:Body/ns:addResponse" );
when I use the above XPath, I got the following exception:
Exception: XPath expression uses unbound namespace prefix ns1
I found ways to do this: remove namespace, which is not recommended.
Any help will be appreciated. Thanks
BTW, is there a better way or toolkit for this work?

Upvotes: 1

Views: 2106

Answers (1)

forty-two
forty-two

Reputation: 12817

You need to create a org.dom4j.XPath object and attach suitable namespace bindings to it. Something like this:

XPath xpath = document.createXPath("/*/soapenv:Envelope/soapenv:Body/ns:addResponse");
Map<String, String> nsb = new HashMap<String, String>();
nsb.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
nsb.put("ns", ".....");
Node node = xpath.selectSingleNode(document);

Is there a better way? Well, these days there is JAX-WS, and if there is WSDL available for your service, it is usually very simple to generate Java interfaces and classes for it.

Upvotes: 1

Related Questions