java axis2解析xml(wsdl返回List数据Map<String,String>

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 webServiceMapString {
    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/getMapString"));
        OMElement result = service.sendReceive(buildParam("getMapString",
                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");
        List<SysUser> list = new ArrayList<SysUser>();
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            NodeList userList = node.getChildNodes();
            String key = "";
            String value = "";
            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) {
                        value = user.getFirstChild().getNodeValue();
                    }
                }
            }
            map.put(key, value);
        }
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println("key:======"+key + "===value:=== " + map.get(key));
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>

<ns:getMapStringResponse 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>userName</map:key>
      <map:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">张三</map:value>
    </map:entry>
    <map:entry xmlns:map="http://ws.apache.org/namespaces/axis2/map" xmlns:ax23="http://webservice.elgin.com/xsd">
      <map:key>userAge</map:key>
      <map:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">20</map:value>
    </map:entry>
  </ns:return>
</ns:getMapStringResponse>

 

posted on 2017-12-08 13:04  火玉  阅读(499)  评论(0编辑  收藏  举报