用wsdl4j来解析一个服务的wsdl文档,如何可以得到服务的名字和服务方法的名字?
如下代码:
String wsdlURL = "http://127.0.0.1:8080/axis/HelloService.jws?wsdl ";
WSDLFactory wsdlFactory;
try {
wsdlFactory = WSDLFactoryImpl.newInstance();
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
wsdlReader.setFeature( "javax.wsdl.verbose ",false);
wsdlReader.setFeature( "javax.wsdl.importDocuments ",true);
Definition def = wsdlReader.readWSDL(null,wsdlURL);
//显示结果
System.out.println( "element: "+def.getPortTypes());
这里只能得到wsdl的getPortTypes的标签,但是它里面的方法名和服务名却不知道如何得到,十在是头痛的事情啊!
------解决方案--------------------void analysisWSDL() throws Exception{
operations=new Hashtable();
WSDLFactory factory = WSDLFactory .newInstance();
WSDLReader reader = factory.newWSDLReader();
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(true);
DocumentBuilder builder = fac.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(wsdl.getBytes( "UTF-8 ")));
Definition def = reader.readWSDL(null,doc);
//一个WSDL可以定义多个服务,我只取了第一个
Service service = (Service) getFirstItem(def.getServices());
Map ports = service.getPorts();
Iterator iter = ports.values().iterator();
String url=null;
Port port=null;
ExtensibilityElement tmp=null;
while(iter.hasNext())
{
port = (Port)iter.next();
tmp= (ExtensibilityElement)port.getExtensibilityElements().get(0);
url = getLocationURI(tmp);
if(url!=null)
break;
}
//修改
soapVersion = getSoapVersion(tmp.getElementType().getNamespaceURI());
//多个接口的问题?
Binding binding = port.getBinding();
List operationList = binding.getBindingOperations();
int size = operationList.size();
operations = new Hashtable(size);
for (int i = 0; i < size; i++) {
BindingOperation oper = (BindingOperation) operationList.get(i);
addOperation(analysisOperation(oper));
}
}
private Object getFirstItem(Map map) {
Iterator keys = map.keySet().iterator();
return map.get(keys.next());
}
private WSOperation analysisOperation(BindingOperation operation) throws Exception{
WSOperation wsoper = new WSOperation();
wsoper.setName(operation.getName());
ExtensibilityElement tmp = (ExtensibilityElement) operation.getExtensibilityElements().get(
0);
wsoper.setSoapAction(getSoapActionURI(tmp));
BindingInput bindingin = operation.getBindingInput();
if (bindingin != null) {
ExtensibilityElement body = (ExtensibilityElement) bindingin.getExtensibilityElements().get(0);
String namespace = getNamespaceURI(body);
Input in = operation.getOperation().getInput();
Map parts = in.getMessage().getParts();
Iterator keys = parts.keySet().iterator();
while (keys.hasNext()) {
Part part = (Part) parts.get(keys.next());
WSParameter para = new WSParameter();
para.setName(part.getName());
para.setDirection( "in ");
para.setNamespace(namespace);
QName type = part.getElementName();
if(type==null)
type=part.getTypeName();
para.setType(type.toString());
wsoper.addParameter(para);