【轉帖】利用wsdl4j解析WSDL文件

http://hi.baidu.com/wxmsona/blog/item/ddce8c25e70f2139c9955956.html

 

利用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:

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文件的服務名,操作接口名,服務地址等等。。。

陸續還會推出解析complexType的自定義類型、。。

今天就到此了。。

發佈了16 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章