Access XML from 3rd-party SOAP service?
I'm using a vendor-created SOAP client to access their SOAP service in my Jersey 1.3 REST application.
In certain cases, I would like like to access the response's XML, instead of the client's proxy class. Is there a way to do this?
I also have access to their WSDL if that would make this easi开发者_如何学编程er to do.
You can use JAX-WS Dispatch client to put your hands on XML:
import java.io.FileInputStream;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import org.w3c.dom.Node;
public class DispatchClient {
public static void main(String[] args) throws Exception {
String wsdlAddress = "http://127.0.0.1:8080/news/NewsWS?wsdl";
URL wsdl = new URL(wsdlAddress);
QName serviceName = new QName("http://news/", "NewsWebService");
QName portName = new QName("http://news/", "NewsPort");
//nie ma WSDL-a
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP12HTTP_BINDING, "http://127.0.0.1:8080/news/NewsWS");
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName,
SOAPMessage.class, Service.Mode.MESSAGE);
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage req = mf.createMessage(null, new FileInputStream("NewsCountSoapRequest.xml"));
SOAPMessage res = dispatch.invoke(req);
SOAPBody body = res.getSOAPBody(); //SOAP body XML
}
}
You can work with SOAP body XML using DOM interface (all these Node madness) or use XPath.
精彩评论