利用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. }catch(WSDLException e){e.printStackTrace();}
  64. }
  65. }

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

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

最新文章

  1. JavaScript面向对象的程序设计
  2. tfs 删除工作区
  3. [LintCode] Trapping Rain Water
  4. Rman备份的保留策略(retention policy)
  5. leetcode 141. Linked List Cycle ----- java
  6. 【一步一图】:详解IIS日志配置
  7. hdu-5505(数论)
  8. mongo学习整理
  9. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
  10. linux中probe函数中传递的参数来源(上)
  11. ZOJ3774 Power of Fibonacci 斐波那契、二项式定理
  12. H5采集pcm流转换采样率实时发送到服务端
  13. 基于FeignClient提供简单的用户查询服务
  14. HTML5新特性[ Notifications ] 桌面消息
  15. 第9月第15天 设计模式 adapter mvc
  16. sencha touch 在视图中显示一个html页面
  17. DDD Quickly - 读书笔记
  18. 【错误记录】uwsgi 启动 flask 出错
  19. R包安装的正确方式
  20. Pylons Controller里面Session.commit()总是出现rollback

热门文章

  1. GIMP工具箱的自定义操作
  2. Controller View 模式
  3. django第六天(模板相关,过滤器和标记)
  4. shell中的$(( )) 的用途:主要用在整数的运算$(( a+b*c ))
  5. Educational Codeforces Round 31- D. Boxes And Balls
  6. JavaScript正则表达式-重复次数(数量词)
  7. UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0xae in position 167: illegal multibyte sequence
  8. 【01】报错:webpack 不是内部或不可执行命令
  9. Eclipse安装以及安装时遇到的问题解决办法
  10. MHA的介绍和测试(一)