• 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>
  • 相关阅读:
    MySQL数据库封装和分页查询
    程序员的价值在哪里?
    奇葩的程序员
    京东咚咚架构演进
    程序员必看的《黑客帝国》,你看懂了吗?
    微信后台技术“干货们”带来的启发
    drf框架 2 drf框架的请求生命周期(as_view和dispatch方法), 请求、解析、渲染、响应、异常, 序列化组件 ,ORM配置回顾(media文件配置),应用在settings.py中INSTALLED_APPS注册意义 ,数据库配置
    drf框架, 接口(api) Django FBV => CBV drf框架的基础试图类 drf核心组件 群查与单查 python换源
    前端Vue框架 05 第三方插件(vuex: 组件间交互的(移动端), axios
    前端Vue框架 04 路由:逻辑跳转、路由传参 项目组件的数据局部化处理data(){ return{} } 组件的生命周期钩子 组件间通信 全局配置css, js
  • 原文地址:https://www.cnblogs.com/bingrong/p/8004899.html
Copyright © 2020-2023  润新知