开发者

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:开发者_JS百科 remove namespace, which is not recommended.

Any help will be appreciated. Thanks

BTW, is there a better way or toolkit for this work?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜