java axis2解析xml(wsdl返回List数据)

package com.elgin.webservice.axis2;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

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 webServiceTest {
    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/getAllUser"));
        OMElement result = service.sendReceive(buildParam("getAllUser",
                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("ns:return");
        List<SysUser> list = new ArrayList<SysUser>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            NodeList userList = node.getChildNodes();
            SysUser sysUser = new SysUser();
            for (int j = 0; j < userList.getLength(); j++) {
                Node user = userList.item(j);
                if ("ax21:age".equals(user.getNodeName())) {
                    if (user.getFirstChild() !=null) {
                        sysUser.setAge(user.getFirstChild().getNodeValue());
                    }
                }
                if ("ax21:idCard".equals(user.getNodeName())) {
                    if (user.getFirstChild() !=null) {
                        sysUser.setIdCard(user.getFirstChild().getNodeValue());
                    }
                }
                if ("ax21:userName".equals(user.getNodeName())) {
                    if (user.getFirstChild() !=null) {
                        sysUser.setUserName(user.getFirstChild().getNodeValue());
                    }
                }
            }
            list.add(sysUser);
        }
        
        for (SysUser sysUser:list) {
            System.out.println("age:=="+sysUser.getAge()+"===idCard:===="+sysUser.getIdCard()+"====userName:==="+sysUser.getUserName());
        }
    }

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

<ns:getAllUserResponse xmlns:ns="http://webservice.elgin.com" xmlns:ax21="http://webservice.elgin.com/xsd">
  <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:SysUser">
    <ax21:age>10</ax21:age>
    <ax21:idCard>411325199212101023</ax21:idCard>
    <ax21:userName>张一</ax21:userName>
  </ns:return>
  <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:SysUser">
    <ax21:age>11</ax21:age>
    <ax21:idCard>421325199212101022</ax21:idCard>
    <ax21:userName>张二</ax21:userName>
  </ns:return>
  <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:SysUser">
    <ax21:age>12</ax21:age>
    <ax21:idCard>431325199212101023</ax21:idCard>
    <ax21:userName>张三</ax21:userName>
  </ns:return>
</ns:getAllUserResponse>

 

posted on 2017-12-07 17:10  火玉  阅读(2538)  评论(0编辑  收藏  举报