2011-10-01 98 views
1

我正在使用供應商創建的SOAP客戶端來訪問我的Jersey 1.3 REST應用程序中的SOAP服務。從第三方SOAP服務訪問XML?

在某些情況下,我想訪問響應的XML,而不是客戶端的代理類。有沒有辦法做到這一點?

我也可以訪問他們的WSDL,如果這可以讓這更容易做到。

回答

0

您可以使用JAX-WS Dispatch客戶端把你的手放在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 
    } 
} 

您可以使用DOM接口(所有這些節點瘋狂)或使用XPath SOAP體的XML工作。