以前从网上找了一个解析wsdl的程序,非常老用的jar包也是02年或者以前的导致一些服务不能解析为此想了很多办法,更新wsdl4j的jar包什么的都试过,结果都失败了。
今天就试着使用soapui的jar来解析,因为发现soapui是啥都能解析的。
开始写代码:
import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
import com.eviware.soapui.model.iface.Operation;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class ImportWsdl {
public static Map> result = new HashMap();
public List getProBySoap(String address) throws Exception {
URL url = new URL(address);
InputStream openStream = url.openStream();
openStream.close();
Map result = new HashMap();
WsdlProject project = new WsdlProject();
WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, address);
WsdlInterface wsdl = wsdls[0];
List operationList = wsdl.getOperationList();
for (int i = 0; i < operationList.size(); i++) {
Operation operation = operationList.get(i);
WsdlOperation op = (WsdlOperation) operation;
Map tmp = new HashMap();
tmp.put(op.getName(), op.createRequest(true));
result.put(i+"", tmp);
}
if ((result != null) && (result.size() != 0)) {
List importWsdl = new ArrayList();
Set keySet = result.keySet();
Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
WsMethodInfo info = new WsMethodInfo();
List inputType = new ArrayList();
List inputNames = new ArrayList();
List isparent = new ArrayList();
String next = ""+ iterator.next();
HashMap hashMap = (HashMap) result.get(next);
Set keySet2 = hashMap.keySet();
Iterator iterator2 = keySet2.iterator();
if (iterator2.hasNext()) {
String next2 = (String) iterator2.next();
String string = (String) hashMap.get(next2);
//处理targetNameSpace
String qname=string.substring(string.lastIndexOf("\"http://")+1,string.lastIndexOf("\">"));
info.setTargetNameSpace(qname);
String soap11 = "http://schemas.xmlsoap.org/soap/envelope";
String soap12 = "http://www.w3.org/2003/05/soap-envelope";
InputStreamReader is = new InputStreamReader(new ByteArrayInputStream(string.getBytes("utf-8")));
BufferedReader ibr = new BufferedReader(is);
String readLine = ibr.readLine();
if (readLine != null) {
if (readLine.indexOf(soap11) >= 0)
info.setTargetXsd("11");
else if (readLine.indexOf(soap12) >= 0) {
info.setTargetXsd("12");
}
}
ibr.close();
is.close();
info.setMethodSoapAction(string);
Document read = DocumentHelper.parseText(string);
Element rootElement = read.getRootElement();
List elements = rootElement.elements();
for (Element element : elements) {
if ("Body".equals(element.getName())) {
List elements2 = element.elements();
info.setMethodName(next2);
for (Element element2 : elements2) {
getParameter(element2, 1, 1, inputType, inputNames, isparent);
info.setInputNames(inputNames);
info.setInputType(inputType);
info.setOutputType(isparent);
}
}
}
importWsdl.add(info);
}
}
return importWsdl;
}
return null;
}
public void getParameter(Element element2, int gen, int genParent, List inputType, List inputNames,
List isparent) {
if (element2 != null) {
List elements3 = element2.elements();
if ((elements3 != null) && (elements3.size() != 0))
for (Element element : elements3) {
inputType.add(gen + "," + genParent);
inputNames.add(element.getQualifiedName());
if (element != null) {
List e = element.elements();
if ((e != null) && (e.size() != 0)) {
isparent.add("1");
int gen1 = gen + gen;
getParameter(element, gen1, gen, inputType, inputNames, isparent);
} else {
isparent.add("0");
}
}
}
}
}
}
WsMethodInfo.java
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
public class WsMethodInfo
{
private String methodName;
private String methodDesc;
private Map methodName2InputParam = new HashMap();
private String targetNameSpace;
private String methodSoapAction;
private String endPoint;
private String targetXsd;
private List inputNames;
private List inputType;
private List inputDesc;
private List outputNames;
private List outputType;
private String sep = "#";
public String getMethodName()
{
return this.methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public List getInputNames() {
return this.inputNames;
}
public void setInputNames(List inputNames) {
this.inputNames = inputNames;
}
public Map getMethodName2InputParam() {
return this.methodName2InputParam;
}
public void setMethodName2InputParam(Map methodName2InputParam) {
this.methodName2InputParam = methodName2InputParam;
}
public String getTargetNameSpace() {
return this.targetNameSpace;
}
public void setTargetNameSpace(String targetNameSpace) {
this.targetNameSpace = targetNameSpace;
}
public String getMethodSoapAction() {
return this.methodSoapAction;
}
public void setMethodSoapAction(String methodSoapAction) {
this.methodSoapAction = methodSoapAction;
}
public String getEndPoint() {
return this.endPoint;
}
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
public String getTargetXsd() {
return this.targetXsd;
}
public void setTargetXsd(String targetXsd) {
this.targetXsd = targetXsd;
}
public List getOutputNames() {
return this.outputNames;
}
public void setOutputNames(List outputNames) {
this.outputNames = outputNames;
}
public List getInputType() {
return this.inputType;
}
public void setInputType(List inputType) {
this.inputType = inputType;
}
public List getOutputType() {
return this.outputType;
}
public void setOutputType(List outputType) {
this.outputType = outputType;
}
public List getInputDesc() {
return this.inputDesc;
}
public void setInputDesc(List inputDesc) {
this.inputDesc = inputDesc;
}
public String madeNewString()
{
StringBuffer su = new StringBuffer();
su.append(this.methodName);
su.append(this.sep);
su.append(this.inputType == null ? "" : StringUtils.join(this.inputType.toArray(), "@"));
su.append(this.sep);
su.append(this.inputNames == null ? "" : StringUtils.join(this.inputNames.toArray(), "@"));
su.append(this.sep);
su.append(this.methodDesc == null ? "" : this.methodDesc);
su.append(this.sep);
su.append(this.sep);
su.append(this.methodSoapAction == null ? "" : this.methodSoapAction);
su.append(this.sep);
su.append(this.outputType == null ? "" : StringUtils.join(this.outputType.toArray(), "@"));
su.append(this.sep);
su.append(this.targetXsd == null ? "" : this.targetXsd);
return su.toString();
}
public String getMethodDesc() {
return this.methodDesc;
}
public void setMethodDesc(String methodDesc) {
this.methodDesc = methodDesc;
}
}