WSDL4J解析WSDL文件方法

利用wsdl4j解析WSDL文件

工具:wsdl4j1.6

解析wsdl文件是axis1.4的服务wsdl文件

wsdl文件:

 

xml version="1.0" encoding="UTF-8" ?>
-   <wsdl:definitions targetNamespace="http://localhost:8080/axis/services/SayHelloService" 
xmlns:apachesoap="http://xml.apache.org/xml-soap" 
xmlns:impl="http://localhost:8080/axis/services/SayHelloService" 
xmlns:intf="http://localhost:8080/axis/services/SayHelloService" 
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:message name="sayHelloResponse">
  <wsdl:part name="sayHelloReturn" type="xsd:string" />
wsdl:message>
-  <wsdl:message name="sayHelloRequest">
  <wsdl:part name="name" type="xsd:string" />
wsdl:message>
-  <wsdl:portType name="SayHello">
-  <wsdl:operation name="sayHello" parameterOrder="name">
  <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />
  <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />
wsdl:operation>
wsdl:portType>
-  <wsdl:binding name="SayHelloServiceSoapBinding" type="impl:SayHello">
  <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://hello.com" use="encoded" />
wsdl:input>
<wsdl:output name="sayHelloResponse">
  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/services/SayHelloService" use="encoded" />
wsdl:output>
wsdl:operation>
wsdl:binding>
-   <wsdl:service name="SayHelloService">
 -  <wsdl:port binding="impl:SayHelloServiceSoapBinding" name="SayHelloService"
  <wsdlsoap:address location="http://localhost:8080/axis/services/SayHelloService" />
wsdl:port>
wsdl:service>

wsdl:definitions>

 

下面是两个程序wsdl4j编写:

程序1:

view plain copy to clipboard print ?
  1. package com.wxm;  
  2. import javax.wsdl.*;  
  3. import javax.wsdl.factory.*;  
  4. import javax.wsdl.xml.*;  
  5. public class ReadWsdl {  
  6. public static void main(String[]args)  
  7. {  
  8. try{  
  9. WSDLFactory factory=WSDLFactory.newInstance();  
  10. WSDLReader reader=factory.newWSDLReader();  
  11. reader.setFeature("javax.wsdl.verbose",true);  
  12. reader.setFeature("javax.wsdl.importDocuments",true);  
  13. Definition def=reader.readWSDL("http://localhost:8080/axis/services/SayHelloService?wsdl");  
  14. WSDLWriter writer=factory.newWSDLWriter();  
  15. writer.writeWSDL(def, System.out);  
  16. }catch(WSDLException e){e.printStackTrace();}   
  17. }  
  18. }  

 

程序2:

view plain copy to clipboard print ?
  1. package com.wxm;  
  2. import javax.wsdl.*;  
  3. import javax.wsdl.extensions.*;  
  4. import javax.wsdl.factory.*;  
  5. import javax.wsdl.xml.*;  
  6. import javax.xml.namespace.QName;  
  7. import java.util.*;  
  8. import org.w3c.dom.*;  
  9. public class NavigatingWSDL {  
  10. public static void main(String[]args)  
  11. {  
  12. try{  
  13.    WSDLFactory factory=WSDLFactory.newInstance();  
  14.    WSDLReader reader=factory.newWSDLReader();  
  15.    reader.setFeature("javax.wsdl.verbose",true);  
  16.    reader.setFeature("javax.wsdl.importDocuments",true);  
  17.    Definition def=reader.readWSDL("http://localhost:8080/axis/services/SayHelloService?wsdl");  
  18.          //解析服务名   
  19.    System.out.println("----------");  
  20.    System.out.println("nService Name:");  
  21.    String tns="http://localhost:8080/axis/services/SayHelloService";  
  22.         Service service =def.getService(new QName(tns,"SayHelloService"));  
  23.    System.out.println(service.getQName().getLocalPart());  
  24.    //解析接口方法名  
  25.    System.out.println("nOperation Name:");  
  26.    Port port =service.getPort("SayHelloService");  
  27.    Binding binding=port.getBinding();  
  28.    PortType portType=binding.getPortType();  
  29.    List operations=portType.getOperations();  
  30.    Iterator operIter=operations.iterator();  
  31.    while(operIter.hasNext())  
  32.    {  
  33.     Operation operation=(Operation)operIter.next();  
  34.     if(!operation.isUndefined())  
  35.     {System.out.println(operation.getName()) ;}  
  36.    }  
  37.    //解析消息,输入输出  
  38.    System.out.println("nMessages:");  
  39.    Map messages=def.getMessages();  
  40.    Iterator msgIter=messages.values().iterator();  
  41.    while(msgIter.hasNext())  
  42.    {  
  43.     Message msg=(Message)msgIter.next();  
  44.     if(!msg.isUndefined())  
  45.     {  
  46.      System.out.println(msg.getQName().getLocalPart());  
  47.      Iterator partIter=msg.getParts().values().iterator();  
  48.      while(partIter.hasNext())  
  49.      {  
  50.       Part part=(Part) partIter.next();  
  51.       System.out.print("parameter name:"+part.getName()+"t");  
  52.       System.out.println("parameter type:"+part.getTypeName().getLocalPart());  
  53.      }  
  54.     }       
  55.    }  
  56.    //解析服务地址  
  57.    System.out.println("nService location:");  
  58.    List l=port.getExtensibilityElements();  
  59.    ExtensibilityElement element=(ExtensibilityElement) l.get(0);  
  60.    String s=element.toString();  
  61.      System.out.println(s.substring(s.indexOf("location")));  
  62.      System.out.println("---------");  
  63.     
  64. }catch(WSDLException e){e.printStackTrace();}   
  65. }  
  66. }  

 可以解析出wsdl文件的服务名,操作接口名,服务地址等

 

转:http://blog.sina.com.cn/s/blog_5ee36ce70100nk97.html

转载于:https://www.cnblogs.com/xijin-wu/p/6890058.html

你可能感兴趣的:(WSDL4J解析WSDL文件方法)