axis2 rpc 方式实现游览器返回XML格式自定义,返回的XML结果如:<return>
<api name="api1">
<value></value>
</api>
<api name="api2">
<value></value>
</api>
<api name="api3">
<value></value>
</api>
</return>下面是我的代码
参数:
<?xml version='1.0' encoding='gb2312'?><info><api name='api1'></api><api name='api2'></api><api name='api3'></api></info>
通过while来解析参数XML元素代码,并判断多个API来添加API元素下的value值:
Iterator<OMElement> iter = omelement.getChildElements();
Map<String, String> apiMap=new HashMap<String,String>();
while (iter.hasNext()) {
OMElement node = iter.next();
if(node.getType()==OMElement.ELEMENT_NODE){
if (node.getLocalName().equals("api")) {
String apiName = node.getAttributeValue(new QName("name"));
//TODO返回当前API的XML
if(apiName.equals("api1")||apiName.equals("api2")||apiName.equals("api3")){
apiMap.put("name",apiName);
}
}
}
}
OMFactory factory = OMAbstractFactory.getOMFactory();
//建立doc节点,doc节点会和下面的root节点合并
OMDocument doc = factory.createOMDocument();
//建立root节点
OMElement responseXML = factory.createOMElement(new QName("return"));
OMElement childApiName = factory.createOMElement("api", null);
log.info("apiname MAP::"+apiMap);
for (Map.Entry<String, String> entry : apiMap.entrySet()) {
 childApiName.addAttribute("name", entry.getValue(), null);
}
responseXML.addChild(childApiName);
doc.addChild(responseXML);
return responseXML;我这写的比较粗糙,大家有更好的方法吗?axis2webservicesrpcxmlomelement