【转帖】利用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: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的自定义类型、。。

今天就到此了。。

你可能感兴趣的:(binding,import,iterator,string,service,output)