首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

用wsdl4j来解析一个服务的wsdl文档,怎么可以得到服务的名字和服务方法的名字

2012-01-05 
用wsdl4j来解析一个服务的wsdl文档,如何可以得到服务的名字和服务方法的名字?如下代码:StringwsdlURLhtt

用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);


}
}
BindingOutput bindingout = operation.getBindingOutput();
if (bindingout != null) {
ExtensibilityElement body = (ExtensibilityElement) bindingin.getExtensibilityElements().get(0);
String namespace = getNamespaceURI(body);
Output out = operation.getOperation().getOutput();
Map parts = out.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( "out ");
para.setNamespace(namespace);
QName type = part.getElementName();
if(type==null)
type=part.getTypeName();
para.setType(type.toString());
wsoper.addParameter(para);
}
}
return wsoper;
}

private String getLocationURI(ExtensibilityElement tmp) throws Exception{
if(tmp instanceof SOAP12AddressImpl)
return ((SOAP12AddressImpl)tmp).getLocationURI();
if(tmp instanceof SOAPAddressImpl)
return ((SOAPAddressImpl)tmp).getLocationURI();
else
return null;
//throw new Exception( "不被支持的SOAP类型或错误的WSDL! ");
}

private String getSoapActionURI(ExtensibilityElement tmp) throws Exception{
if(tmp instanceof SOAP12OperationImpl)
return ((SOAP12OperationImpl)tmp).getSoapActionURI();
if(tmp instanceof SOAPOperationImpl)
return ((SOAPOperationImpl)tmp).getSoapActionURI();
else
return " ";
//throw new Exception( "不被支持的SOAP类型或错误的WSDL! ");

}

private String getNamespaceURI(ExtensibilityElement body)throws Exception {
if(body instanceof SOAP12BodyImpl)
return ((SOAP12BodyImpl)body).getNamespaceURI();
if(body instanceof SOAPBodyImpl)
return ((SOAPBodyImpl)body).getNamespaceURI();
else
return " ";
//throw new Exception( "不被支持的SOAP类型或错误的WSDL! ");

}

private int getSoapVersion(String namespace) {
if (namespace.equals( "http://schemas.xmlsoap.org/wsdl/soap/ ")) {
return 11;
}
if (namespace.equals( "http://schemas.xmlsoap.org/wsdl/soap12/ ")) {
return 12;
}
return 0;
}
}

WSDL还是比较复杂的,我很多地方都省了,只取了HTTP-SOAP方式的数据。
[解决办法]
blog 更新:
think in java 各章后练习答案.....
http://blog.csdn.net/heimaoxiaozi/

[解决办法]
mark

热点排行