Java Reference
In-Depth Information
Extracting Content from a SOAP Message
Problem
You have invoked a web service with Dispatch<SOAPMessage> and now you want to work
with an XML view of the message response.
Solution
Use the SOAPMessage response to get the SOAP body, and then invoke its extractCon-
tentAsDocument method. That gives you an org.w3.dom.Document object to work with. You
can then execute XPath queries against it, or just use the DOM 3 API as you otherwise would.
This code is identical to that in the previous example, except that instead of just using the con-
venience method writeTo to send the response to the console, you will get a DOM view of
the response and then pull out the author's last name. Example 5-5 shows how this works.
Example5-5.Getting a DOM view of a SOAP response
public void extractDOMFromSOAPResult() {
try {
URL wsdl =
new URL("http://localhost:8080/CatalogService/
Catalog?wsdl");
String ns = "http://ns.soacookbook.com/ws/catalog";
//Create the Service name
String svcName = "CatalogService";
QName svcQName = new QName(ns, svcName);
//Get a delegate wrapper
Service service = Service.create(wsdl, svcQName);
//Create the Port name
String portName = "CatalogPort";
QName portQName = new QName(ns, portName);
Dispatch<SOAPMessage> dispatch =
service.createDispatch(portQName,
SOAPMessage.class, Service.Mode.MESSAGE);
//create the message
SOAPMessage soapMsg =
Search WWH ::




Custom Search