如下代码:
String wsdlURL = "http://127.0.0.1:8080/axis/HelloService.jws?wsdl";
WSDLFactory wsdlFactory;
try {
wsdlFactory = WSDLFactoryImpl.newInstance();
WSDLReader wsdlReader  =  wsdlFactory.newWSDLReader();
wsdlReader.setFeature("javax.wsdl.verbose",false);
wsdlReader.setFeature("javax.wsdl.importDocuments",true);
Definition def = wsdlReader.readWSDL(null,wsdlURL);
 
//显示结果
System.out.println("element: "+def.getPortTypes());这里只能得到wsdl的getPortTypes的标签,但是它里面的方法名和服务名却不知道如何得到,十在是头痛的事情啊!

解决方案 »

  1.   

    这个wsdl文档:
     <?xml version="1.0" encoding="UTF-8" ?> 
    - <wsdl:definitions targetNamespace="http://127.0.0.1:8080/axis/HelloService.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/axis/HelloService.jws" xmlns:intf="http://127.0.0.1:8080/axis/HelloService.jws" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <!-- 
    WSDL created by Apache Axis version: 1.3
    Built on Oct 05, 2005 (05:23:37 EDT)  --> 
    - <wsdl:message name="sayHelloResponse">
      <wsdl:part name="sayHelloReturn" type="xsd:string" /> 
      </wsdl:message>
    - <wsdl:message name="sayHelloRequest">
      <wsdl:part name="username" type="xsd:string" /> 
      </wsdl:message>
    - <wsdl:portType name="HelloService">
    - <wsdl:operation name="sayHello" parameterOrder="username">
      <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" /> 
      <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" /> 
      </wsdl:operation>
      </wsdl:portType>
    - <wsdl:binding name="HelloServiceSoapBinding" type="impl:HelloService">
      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="sayHello">
      <wsdlsoap:operation soapAction="" /> 
    - <wsdl:input name="sayHelloRequest">
      <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded" /> 
      </wsdl:input>
    - <wsdl:output name="sayHelloResponse">
      <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/axis/HelloService.jws" use="encoded" /> 
      </wsdl:output>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:service name="HelloServiceService">
    - <wsdl:port binding="impl:HelloServiceSoapBinding" name="HelloService">
      <wsdlsoap:address location="http://127.0.0.1:8080/axis/HelloService.jws" /> 
      </wsdl:port>
      </wsdl:service>
      </wsdl:definitions>
      

  2.   

    void analysisWSDL()  throws Exception{
          operations=new Hashtable();
          WSDLFactory factory = WSDLFactory .newInstance();
          WSDLReader reader = factory.newWSDLReader();
         DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
         fac.setNamespaceAware(true);
         DocumentBuilder builder = fac.newDocumentBuilder();
         Document doc = builder.parse(new ByteArrayInputStream(wsdl.getBytes("UTF-8")));     Definition def = reader.readWSDL(null,doc);      //一个WSDL可以定义多个服务,我只取了第一个
          Service service = (Service) getFirstItem(def.getServices());
          Map ports = service.getPorts();
          Iterator iter = ports.values().iterator();
          String url=null;
          Port port=null;
          ExtensibilityElement tmp=null;
          while(iter.hasNext())
          {
             port = (Port)iter.next();
             tmp= (ExtensibilityElement)port.getExtensibilityElements().get(0);
             url = getLocationURI(tmp);
             if(url!=null)
               break;
          }
          //修改
          soapVersion = getSoapVersion(tmp.getElementType().getNamespaceURI());
          //多个接口的问题?
          Binding binding = port.getBinding();
          List operationList = binding.getBindingOperations();
          int size = operationList.size();
          operations = new Hashtable(size);
          for (int i = 0; i < size; i++) {
            BindingOperation oper = (BindingOperation) operationList.get(i);
            addOperation(analysisOperation(oper));
          }
      }  private Object getFirstItem(Map map) {
        Iterator keys = map.keySet().iterator();
        return map.get(keys.next());
      }  private WSOperation analysisOperation(BindingOperation operation) throws Exception{
        WSOperation wsoper = new WSOperation();
        wsoper.setName(operation.getName());
        ExtensibilityElement tmp = (ExtensibilityElement) operation.getExtensibilityElements().get(
            0);
        wsoper.setSoapAction(getSoapActionURI(tmp));    BindingInput bindingin = operation.getBindingInput();
        if (bindingin != null) {
          ExtensibilityElement body = (ExtensibilityElement) bindingin.getExtensibilityElements().get(0);
          String namespace = getNamespaceURI(body);
          Input in = operation.getOperation().getInput();
          Map parts = in.getMessage().getParts();
          Iterator keys = parts.keySet().iterator();
          while (keys.hasNext()) {
            Part part = (Part) parts.get(keys.next());
            WSParameter para = new WSParameter();
            para.setName(part.getName());
            para.setDirection("in");
            para.setNamespace(namespace);
            QName type = part.getElementName();
            if(type==null)
              type=part.getTypeName();
            para.setType(type.toString());
            wsoper.addParameter(para);
          }
        }
        BindingOutput bindingout = operation.getBindingOutput();
        if (bindingout != null) {
          ExtensibilityElement body = (ExtensibilityElement) bindingin.getExtensibilityElements().get(0);
          String namespace = getNamespaceURI(body);
          Output out = operation.getOperation().getOutput();
          Map parts = out.getMessage().getParts();
          Iterator keys = parts.keySet().iterator();
          while (keys.hasNext()) {
            Part part = (Part) parts.get(keys.next());
            WSParameter para = new WSParameter();
            para.setName(part.getName());
            para.setDirection("out");
            para.setNamespace(namespace);
            QName type = part.getElementName();
            if(type==null)
              type=part.getTypeName();
            para.setType(type.toString());
            wsoper.addParameter(para);
          }
        }
        return wsoper;
      }  private String getLocationURI(ExtensibilityElement tmp) throws Exception{
        if(tmp instanceof SOAP12AddressImpl)
          return ((SOAP12AddressImpl)tmp).getLocationURI();
        if(tmp instanceof SOAPAddressImpl)
          return ((SOAPAddressImpl)tmp).getLocationURI();
        else
          return null;
          //throw new Exception("不被支持的SOAP类型或错误的WSDL!");
      }  private String getSoapActionURI(ExtensibilityElement tmp) throws Exception{
        if(tmp instanceof SOAP12OperationImpl)
          return ((SOAP12OperationImpl)tmp).getSoapActionURI();
        if(tmp instanceof SOAPOperationImpl)
          return ((SOAPOperationImpl)tmp).getSoapActionURI();
        else
          return "";
          //throw new Exception("不被支持的SOAP类型或错误的WSDL!");  }  private String getNamespaceURI(ExtensibilityElement body)throws Exception {
        if(body instanceof SOAP12BodyImpl)
          return ((SOAP12BodyImpl)body).getNamespaceURI();
        if(body instanceof SOAPBodyImpl)
          return ((SOAPBodyImpl)body).getNamespaceURI();
        else
          return "";
          //throw new Exception("不被支持的SOAP类型或错误的WSDL!");  }  private int getSoapVersion(String namespace) {
        if (namespace.equals("http://schemas.xmlsoap.org/wsdl/soap/")) {
          return 11;
        }
        if (namespace.equals("http://schemas.xmlsoap.org/wsdl/soap12/")) {
          return 12;
        }
        return 0;
      }
    }WSDL还是比较复杂的,我很多地方都省了,只取了HTTP-SOAP方式的数据。
      

  3.   

    to:fhuc(1.7)
    很感谢你的回复对你的代码有如下不明处:
    Document doc = builder.parse(new ByteArrayInputStream(wsdl.getBytes("UTF-8")));这句话中的“wsdl”的定义在那里啊,编译时报错。
    WSOperation wsoper = new WSOperation();
    WSParameter para = new WSParameter();
    这两个实例的类也没有,编译也报错。
    SOAP12AddressImpl
    SOAP12OperationImpl
    SOAP12BodyImpl
    也都报错啊!下面是我加的引用:(是.jar包缺少吗?)
    import java.io.ByteArrayInputStream;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import javax.wsdl.Binding;
    import javax.wsdl.BindingInput;
    import javax.wsdl.BindingOperation;
    import javax.wsdl.BindingOutput;
    import javax.wsdl.Definition;
    import javax.wsdl.Input;
    import javax.wsdl.Output;
    import javax.wsdl.Part;
    import javax.wsdl.Port;
    import javax.wsdl.Service;
    import javax.wsdl.extensions.ExtensibilityElement;
    import javax.wsdl.factory.WSDLFactory;
    import javax.wsdl.xml.WSDLReader;
    import javax.xml.namespace.QName;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
    import com.ibm.wsdl.extensions.soap.SOAPBodyImpl;
      

  4.   

    blog 更新:
     think in java 各章后练习答案.....
     http://blog.csdn.net/heimaoxiaozi/
      

  5.   

    to:heimaoxiaozi(小木公子) 
    这些是什么啊!
      

  6.   

    wsdl 就是WSDL文件内容,我只是从自己的解析程序中截出部分,你对照API就可以看懂了
    我用的是wsdl4j-1_6
      

  7.   

    to:fhuc(1.7) 
    你能把代码加全吗,这样很难看懂啊。
    或者给一些中文的说明也可以啊。拜托拉
      

  8.   

    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.List;
    import javax.wsdl.*;
    import javax.wsdl.extensions.soap.*;
    import javax.wsdl.factory.*;
    import javax.wsdl.xml.*;import java.awt.*;
    import javax.swing.*;import org.xml.sax.*;
    import com.ibm.wsdl.factory.*;
    import org.apache.xerces.util.SAXInputSource;
    import org.apache.xerces.jaxp.DocumentBuilderImpl;
    import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.Document;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.wsdl.extensions.ExtensibilityElement;
    import com.ibm.wsdl.extensions.soap12.SOAP12AddressImpl;
    import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
    import com.ibm.wsdl.extensions.soap12.SOAP12OperationImpl;
    import com.ibm.wsdl.extensions.soap.SOAPOperationImpl;
    import com.ibm.wsdl.extensions.soap12.SOAP12BodyImpl;
    import com.ibm.wsdl.extensions.soap.SOAPBodyImpl;
    import com.ibm.wsdl.extensions.http.HTTPAddressImpl;
    import javax.xml.namespace.QName;
    void analysisWSDL() throws Exception{
    WSDLFactory factory = WSDLFactory .newInstance();
    WSDLReader reader = factory.newWSDLReader();
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setNamespaceAware(true);
    DocumentBuilder builder = fac.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(wsdl.getBytes("UTF-8")));  //wsdl 就是我的WSDL文件的内容。Definition def = reader.readWSDL(null,doc);//一个WSDL可以定义多个服务,我只取了第一个
    Service service = (Service) getFirstItem(def.getServices());  //service就是第一个定义的服务的对象。
    Map ports = service.getPorts();   //ports:服务对应的接口,服务可能有多个接口,我只取其中的HTTP SOAP接口。可能是HTTP GET,HTTP POST,HTTP SOAP方式等等。即此服务支持的访问方式。
    Iterator iter = ports.values().iterator();
    String url=null;
    Port port=null;
    ExtensibilityElement tmp=null;
    while(iter.hasNext())
    {
    port = (Port)iter.next();
    tmp= (ExtensibilityElement)port.getExtensibilityElements().get(0);
    url = getLocationURI(tmp);  //url:服务访问的地址,getLocationURI()过滤了其他方式,所以现在port即为HTTP PORT方式的接口对象。
    if(url!=null)
    break;
    }soapVersion = getSoapVersion(tmp.getElementType().getNamespaceURI()); //此服务的SOAP标准,可能是SOAP 1.1或SOAP 1.2,以后可能有其他的。
    //多个接口的问题?
    Binding binding = port.getBinding(); //binding:包含了接口访问的定义。
    List operationList = binding.getBindingOperations();  //operationList 可以理解为服务提供的方法的列表。
    int size = operationList.size();
    for (int i = 0; i < size; i++) {
    BindingOperation oper = (BindingOperation) operationList.get(i);
    analysisOperation(oper);      //逐个分析方法对象。
    }
    }private Object getFirstItem(Map map) {
    Iterator keys = map.keySet().iterator();
    return map.get(keys.next());
    }private WSOperation analysisOperation(BindingOperation operation) throws Exception{operation.getName();  //方法的名称。
    ExtensibilityElement tmp = (ExtensibilityElement) operation.getExtensibilityElements().get(
    0);                       
    getSoapActionURI(tmp);  //访问方法的地址。BindingInput bindingin = operation.getBindingInput();//bindingin 包装了输入参数。
    if (bindingin != null) {
    ExtensibilityElement body = (ExtensibilityElement) bindingin.getExtensibilityElements().get(0);
    String namespace = getNamespaceURI(body);                       //取得参数的名称空间。
    Input in = operation.getOperation().getInput();
    Map parts = in.getMessage().getParts();
    Iterator keys = parts.keySet().iterator();
    while (keys.hasNext()) {
    Part part = (Part) parts.get(keys.next());
    part.getName();               //参数的名称。
    QName type = part.getElementName();
    if(type==null)
    type=part.getTypeName();      //类型。具体类型或者对应的XML SCHEMA元素名。
    }
    }
    BindingOutput bindingout = operation.getBindingOutput();//包装了输出参数。
    if (bindingout != null) {
    ExtensibilityElement body = (ExtensibilityElement) bindingin.getExtensibilityElements().get(0);
    String namespace = getNamespaceURI(body);
    Output out = operation.getOperation().getOutput();
    Map parts = out.getMessage().getParts();
    Iterator keys = parts.keySet().iterator();
    while (keys.hasNext()) {
    Part part = (Part) parts.get(keys.next());
    part.getName();
    QName type = part.getElementName();
    if(type==null)
    type=part.getTypeName();
    }
    }
    }private String getLocationURI(ExtensibilityElement tmp) throws Exception{
    if(tmp instanceof SOAP12AddressImpl)
    return ((SOAP12AddressImpl)tmp).getLocationURI();
    if(tmp instanceof SOAPAddressImpl)
    return ((SOAPAddressImpl)tmp).getLocationURI();
    else
    return null;
    //throw new Exception("不被支持的SOAP类型或错误的WSDL!");
    }private String getSoapActionURI(ExtensibilityElement tmp) throws Exception{
    if(tmp instanceof SOAP12OperationImpl)
    return ((SOAP12OperationImpl)tmp).getSoapActionURI();
    if(tmp instanceof SOAPOperationImpl)
    return ((SOAPOperationImpl)tmp).getSoapActionURI();
    else
    return "";
    //throw new Exception("不被支持的SOAP类型或错误的WSDL!");}private String getNamespaceURI(ExtensibilityElement body)throws Exception {
    if(body instanceof SOAP12BodyImpl)
    return ((SOAP12BodyImpl)body).getNamespaceURI();
    if(body instanceof SOAPBodyImpl)
    return ((SOAPBodyImpl)body).getNamespaceURI();
    else
    return "";
    //throw new Exception("不被支持的SOAP类型或错误的WSDL!");}private int getSoapVersion(String namespace) {
    if (namespace.equals("http://schemas.xmlsoap.org/wsdl/soap/")) {
    return 11;
    }
    if (namespace.equals("http://schemas.xmlsoap.org/wsdl/soap12/")) {
    return 12;
    }
    return 0;
    }
    }
    其实你不用WSDL4J的话可以用wsCaller还好些,到GOOGLE上搜wsCaller就可以了,有源代码的,WSDL4J只能分析到参数的大的类型上,要分析schema的具体类型要其他方法,wsCaller用的是AXIS,可以分析到小的参数,可是好象对SOAP HEADER 支持不好。
      

  9.   

    to:fhuc(1.7) 
    这一句:
    Document doc = builder.parse(new ByteArrayInputStream(wsdl.getBytes("UTF-8")));  //wsdl 就是我的WSDL文件的内容。
    这个wsdl是怎么得到的呢,在上面没有声明啊,它是一个字符串还是一个Xml啊
      

  10.   

    to:fhuc(1.7) 
    这个wsdl是如何得到的呢?
      

  11.   

    try
    {
    WSDLFactory factory = WSDLFactory.newInstance();
    WSDLReader reader = factory.newWSDLReader();
    reader.setFeature(“javax.wsdl.verbose”, true);
    reader.setFeature(“javax.wsdl.importDocuments”, true);
    Definition def = reader.readWSDL(null, "sample.wsdl");
    }
    catch (WSDLException e)
    {
    e.printStackTrace();
    }怎么都可以啊,WSDLReader不是有很多方法么