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 created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
-  <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:

  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:

  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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值