利用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:
- package com.wxm;
- import javax.wsdl.*;
- import javax.wsdl.factory.*;
- import javax.wsdl.xml.*;
- public class ReadWsdl {
- public static void main(String[]args)
- {
- 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("http://localhost:8080/axis/services/SayHelloService?wsdl");
- WSDLWriter writer=factory.newWSDLWriter();
- writer.writeWSDL(def, System.out);
- }catch(WSDLException e){e.printStackTrace();}
- }
- }
程序2:
- package com.wxm;
- import javax.wsdl.*;
- import javax.wsdl.extensions.*;
- import javax.wsdl.factory.*;
- import javax.wsdl.xml.*;
- import javax.xml.namespace.QName;
- import java.util.*;
- import org.w3c.dom.*;
- public class NavigatingWSDL {
- public static void main(String[]args)
- {
- 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("http://localhost:8080/axis/services/SayHelloService?wsdl");
- //解析服务名
- System.out.println("----------");
- System.out.println("nService Name:");
- String tns="http://localhost:8080/axis/services/SayHelloService";
- Service service =def.getService(new QName(tns,"SayHelloService"));
- System.out.println(service.getQName().getLocalPart());
- //解析接口方法名
- System.out.println("nOperation Name:");
- Port port =service.getPort("SayHelloService");
- Binding binding=port.getBinding();
- PortType portType=binding.getPortType();
- List operations=portType.getOperations();
- Iterator operIter=operations.iterator();
- while(operIter.hasNext())
- {
- Operation operation=(Operation)operIter.next();
- if(!operation.isUndefined())
- {System.out.println(operation.getName()) ;}
- }
- //解析消息,输入输出
- System.out.println("nMessages:");
- Map messages=def.getMessages();
- Iterator msgIter=messages.values().iterator();
- while(msgIter.hasNext())
- {
- Message msg=(Message)msgIter.next();
- if(!msg.isUndefined())
- {
- System.out.println(msg.getQName().getLocalPart());
- Iterator partIter=msg.getParts().values().iterator();
- while(partIter.hasNext())
- {
- Part part=(Part) partIter.next();
- System.out.print("parameter name:"+part.getName()+"t");
- System.out.println("parameter type:"+part.getTypeName().getLocalPart());
- }
- }
- }
- //解析服务地址
- System.out.println("nService location:");
- List l=port.getExtensibilityElements();
- ExtensibilityElement element=(ExtensibilityElement) l.get(0);
- String s=element.toString();
- System.out.println(s.substring(s.indexOf("location")));
- System.out.println("---------");
- }catch(WSDLException e){e.printStackTrace();}
- }
- }
可以解析出wsdl文件的服务名,操作接口名,服务地址等
转:http://blog.sina.com.cn/s/blog_5ee36ce70100nk97.html
分类:
webservice
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!