package com.elgin.webservice.axis2;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.elgin.webservice.SysUser;
public class webServiceMapStringList {
static EndpointReference tReference;
static ServiceClient service;
static OMFactory fac;
static OMNamespace omNs;
public static String invokeRemoteFuc() throws AxisFault,
ParserConfigurationException {
String endpoint = "http://localhost:9090/AxisWebDemo/services/myService";
service = new ServiceClient();// 新建一个service
tReference = new EndpointReference(endpoint);
fac = OMAbstractFactory.getOMFactory();
omNs = fac.createOMNamespace("http://webservice.elgin.com", "tns");
service.setOptions(buildOptions("http://webservice.elgin.com/getMapList"));
OMElement result = service.sendReceive(buildParam("getMapList",
new String[] {}, new String[] {}));
return result.toString();// 返回值
}
private static OMElement buildParam(String method, String[] args,
String[] val) {
OMElement data = fac.createOMElement(method, omNs);
for (int i = 0; i < args.length; i++) {
OMElement inner = fac.createOMElement(args[i], omNs);
inner.setText(val[i]);
data.addChild(inner);
}
return data;
}
private static Options buildOptions(String action) {
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setTo(tReference);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setProperty(HTTPConstants.CHUNKED, "false");// 设置不受限制
options.setProperty(Constants.Configuration.HTTP_METHOD,
HTTPConstants.HTTP_METHOD_POST);
options.setAction(action);
return options;
}
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
String result = invokeRemoteFuc();
System.out.println(result); // 输出
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
StringReader stringReader = new StringReader(result.toString());
InputSource inputSource = new InputSource(stringReader);
Document document = documentBuilder.parse(inputSource);
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("map:entry");
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NodeList userList = node.getChildNodes();
String key = "";
List<SysUser> list = new ArrayList<SysUser>();
for (int j = 0; j < userList.getLength(); j++) {
Node user = userList.item(j);
if ("map:key".equals(user.getNodeName())) {
if (user.getFirstChild() != null) {
key = user.getFirstChild().getNodeValue();
}
}
if ("map:value".equals(user.getNodeName())) {
if (user.getFirstChild() != null) {
NodeList nodeMapList = user.getChildNodes();
SysUser sysUser = new SysUser();
for (int h = 0; h < nodeMapList.getLength(); h++) {
Node userMap = nodeMapList.item(h);
if ("ax23:age".equals(userMap.getNodeName())) {
if (userMap.getFirstChild() != null) {
sysUser.setAge(userMap.getFirstChild().getNodeValue());
}
}
if ("ax23:idCard".equals(userMap.getNodeName())) {
if (userMap.getFirstChild() != null) {
sysUser.setIdCard(userMap.getFirstChild().getNodeValue());
}
}
if ("ax23:userName".equals(userMap.getNodeName())) {
if (userMap.getFirstChild() != null) {
sysUser.setUserName(userMap.getFirstChild().getNodeValue());
}
}
}
list.add(sysUser);
}
}
map.put(key, list);
}
}
Set<String> keys = map.keySet();
for (String key : keys) {
List<SysUser> list2 = (List<SysUser>) map.get(key);
System.out.println("key:======" + key + "===value:=== " + list2.size());
for (SysUser sysUser : list2) {
System.out.println(key + "_age:======" + sysUser.getAge() + key
+ "_idCard:=== " + sysUser.getIdCard() + key
+ "_userName:=== " + sysUser.getUserName());
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ns:getMapListResponse xmlns:ns="http://webservice.elgin.com">
<ns:return>
<map:entry xmlns:map="http://ws.apache.org/namespaces/axis2/map" xmlns:ax23="http://webservice.elgin.com/xsd">
<map:key>userList1</map:key>
<map:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="ax23:SysUser">
<ax23:age>10</ax23:age>
<ax23:idCard>411325199212101023</ax23:idCard>
<ax23:userName>张一</ax23:userName>
</map:value>
<map:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="ax23:SysUser">
<ax23:age>11</ax23:age>
<ax23:idCard>421325199212101022</ax23:idCard>
<ax23:userName>张二</ax23:userName>
</map:value>
</map:entry>
<map:entry xmlns:map="http://ws.apache.org/namespaces/axis2/map" xmlns:ax23="http://webservice.elgin.com/xsd">
<map:key>userList2</map:key>
<map:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="ax23:SysUser">
<ax23:age>12</ax23:age>
<ax23:idCard>431325199212101023</ax23:idCard>
<ax23:userName>张三</ax23:userName>
</map:value>
</map:entry>
</ns:return>
</ns:getMapListResponse>