概述
packagecom.elgin.webservice.axis2;importjava.io.IOException;importjava.io.StringReader;importjava.util.ArrayList;importjava.util.List;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.ParserConfigurationException;importorg.apache.axiom.om.OMAbstractFactory;importorg.apache.axiom.om.OMElement;importorg.apache.axiom.om.OMFactory;importorg.apache.axiom.om.OMNamespace;importorg.apache.axiom.soap.SOAP11Constants;importorg.apache.axis2.AxisFault;importorg.apache.axis2.Constants;importorg.apache.axis2.addressing.EndpointReference;importorg.apache.axis2.client.Options;importorg.apache.axis2.client.ServiceClient;importorg.apache.axis2.transport.http.HTTPConstants;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;importorg.xml.sax.InputSource;importorg.xml.sax.SAXException;importcom.elgin.webservice.SysUser;public classwebServiceTest {staticEndpointReference tReference;staticServiceClient service;staticOMFactory fac;staticOMNamespace omNs;public static String invokeRemoteFuc() throwsAxisFault, ParserConfigurationException {
String endpoint= "http://localhost:9090/AxisWebDemo/services/myService";
service= new ServiceClient();//新建一个service
tReference = newEndpointReference(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[] {}, newString[] {}));return result.toString();//返回值
}private staticOMElement 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);
}returndata;
}private staticOptions buildOptions(String action) {
Options options= newOptions();
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);returnoptions;
}public static void main(String[] args) throwsParserConfigurationException, SAXException, IOException {
String result=invokeRemoteFuc();
System.out.println(result);//输出
DocumentBuilderFactory builderFactory =DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder=builderFactory.newDocumentBuilder();
StringReader stringReader= newStringReader(result.toString());
InputSource inputSource= newInputSource(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= newSysUser();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());
}
}
}
最后
以上就是从容水蜜桃为你收集整理的axis解析返回值_java axis2解析xml(wsdl返回List数据)的全部内容,希望文章能够帮你解决axis解析返回值_java axis2解析xml(wsdl返回List数据)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复