博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java axis2解析xml(wsdl返回List数据)
阅读量:5066 次
发布时间:2019-06-12

本文共 5062 字,大约阅读时间需要 16 分钟。

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
list = new ArrayList
(); 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()); } }}
10
411325199212101023
张一
11
421325199212101022
张二
12
431325199212101023
张三

 

转载于:https://www.cnblogs.com/bingrong/p/8000001.html

你可能感兴趣的文章
Item 3 - What is an efficient way to implement a singleton pattern in Java?
查看>>
java中main函数的String[] args
查看>>
在乌班图中将py3设置为默认解释器
查看>>
Vue - 在v-repeat中使用计算属性
查看>>
Keil4 几例异常解决办法
查看>>
hibernate延迟加载(get和load的区别)(转)
查看>>
[Leetcode]@python 68. Text Justification
查看>>
菜鸟运维的悲剧
查看>>
多线程处理海量数据的解决方案
查看>>
DevExpress 中 在做全选的全消功能的时候 加快效率
查看>>
Rotate Image,N*N矩阵顺时针旋转90度
查看>>
获取Web.config配置节
查看>>
易语言人脸识别算法源码
查看>>
eclipse 保存html 提示 save could not be completed
查看>>
微信小程序的z-index在苹果ios无效
查看>>
Enterprise Library 企业库 V4.1
查看>>
C# 调用Word(PrintOut) 直接打印,出现“第一节的页边距设于可打印区域之外,是否继续”...
查看>>
ML in Action 决策树
查看>>
[转]linux下TCP连接占用的资源
查看>>
Python爬虫(五)
查看>>