利用wsdl4j解析wsdl文件的例子

基于WSDL4J.jar写了一个简单的Web服务wsdl文件解析工具,,能够解析大部分与XML Schema规范兼容的Web服务,主要的目的是用来学习WSDL4J这个工具及如何使用它们解析一个Web服务。不多说了,测试代码如下,完整的代码请从附件下载。

package wsclient.test;

import java.util.Iterator;
import java.util.List;

import wsclient.domain.OperationInfo;
import wsclient.domain.ParameterInfo;
import wsclient.domain.ServiceInfo;
import wsclient.util.ComponentBuilder;

public class WSClient {

public static void testWSDL4J() {
int i = 0, j = 0;

try {
ComponentBuilder builder = new ComponentBuilder();
ServiceInfo serviceInfo = new ServiceInfo();
String wsdllocation = "http://localhost:8080/axis/services/Phone?wsdl";
serviceInfo.setWsdllocation(wsdllocation);
serviceInfo = builder.buildserviceinformation(serviceInfo);
System.out.println("");
Iterator iter = serviceInfo.getOperations();
System.out.println("现在可以查看远端Web服务对象的有关情况了(对应本地Web服务类,ServiceInfo)");
System.out.println(serviceInfo.getName() + "提供的操作有:");
while (iter.hasNext()) {
i++;
OperationInfo oper = (OperationInfo) iter.next();
System.out.println("");
System.out.println("操作:" + i + " " + oper.getTargetMethodName());
List inps = oper.getInparameters();
List outps = oper.getOutparameters();
if (inps.size() == 0) {
System.out.println("此操作所需的输入参数为:");
System.out.println("执行此操作不需要输入任何参数!");
} else {
System.out.println("此操作所需的输入参数为:");
for (Iterator iterator1 = inps.iterator(); iterator1.hasNext();) {
ParameterInfo element = (ParameterInfo) iterator1.next();
System.out.println("参数名为:" + element.getName());
System.out.println("参数类型为:" + element.getKind());
}
}
if (outps.size() == 0) {
System.out.println("执行此操作不返回任何参数!");
} else {
System.out.println("此操作的输出参数为:");
for (Iterator iterator2 = outps.iterator(); iterator2.hasNext();) {
ParameterInfo element = (ParameterInfo) iterator2.next();
System.out.println("参数名:" + element.getName());
System.out.println("类型为:" + element.getKind());
}
}
System.out.println("");
}
}

catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}

public static void main(String[] args) {
WSClient.testWSDL4J();
}

}

你可能感兴趣的:(java)