服务端方法:
public static Map<String, String> GETDATA(String xml)
{
Map<String, String> ret_map = new HashMap<String, String>();
ret_map.put("result", ""+result);
ret_map.put("restr", restr);
return ret_map;
}
客户端调用案例:
private static void axis2WebService() {
try {
String xml="";
try {
xml = "<root></root>";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String soapBindingAddress = "http://localhost:8080/axis2/services/Service?wsdl";
ServiceClient sender = new ServiceClient();
EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
Options options = new Options();
options.setTo(endpointReference);
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
// 这个和qname差不多,设置命名空间
OMNamespace omNs = fac.createOMNamespace("http://Service", "svc");
OMElement data = fac.createOMElement("GETDATA", omNs);
// 对应参数的节点
String[] strs = new String[] { "arg0" };
// 参数值
String[] val = new String[] {xml};
for (int i = 0; i < strs.length; i++) {
QName qname=new QName(strs[i]);
OMElement inner = fac.createOMElement(qname);
inner.setText(val[i]);
data.addChild(inner);
}
// 发送数据,返回结果
OMElement result = sender.sendReceive(data);
System.out.println(result.toString());
System.out.println(getResults(result.getFirstElement()).toString());
} catch (AxisFault ex) {
ex.printStackTrace();
}
}
//直接将默认的aar返回MAP的生成的xml 转化成MAP格式
public static Map<String, String> getResults(OMElement element) {
if (element == null) {
return null;
}
Iterator<OMNode> iter = element.getChildElements();
Map<String, String> map = new HashMap<String, String>();
while (iter.hasNext()) {
OMNode omNode = (OMNode) iter.next();
System.out.print(omNode.toString());
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
Iterator<OMNode> iter2 = omElement.getChildElements();
String map_key="";
String map_value="";
while (iter2.hasNext()) {
OMNode omNode2 = (OMNode) iter2.next();
if (omNode2.getType() == omNode2.ELEMENT_NODE) {
OMElement omElement2 = (OMElement) omNode2;
String key = omElement2.getLocalName().trim();
String value = omElement2.getText().trim();
if(key.equals("key")){
map_key=value;
}else if(key.equals("value")){
map_value=value;
}
}
}
map.put(map_key, map_value);
}
}
return map;
}
这样实现以后,字符串就不会出现转义情况