webservice从测试发布及,客户端调用,xml返回结果封装,xml返回结果解析

package com.px.train.util;

 

import java.rmi.RemoteException;

import java.util.List;

 

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import javax.xml.ws.Endpoint;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.XMLType;

import org.apache.commons.lang3.StringUtils;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

 

import com.px.train.model.TrainClassStudent;

import com.px.train.service.impl.ApplyPostServiceImpl;

 

public class MyServer {

 

   public static void main(String[] args) {

     test1();

    }

   /**

    * webservice发布测试方法

    * @param ele

    */

   private static void test1() {

     // TODO Auto-generated method stub

     ApplyPostServiceImpl service=new ApplyPostServiceImpl();

     System.out.println("star");

     String str="",uri="";

        String result=service.applyUserList(uri,str);

        System.out.println(result);

        Endpoint.publish("http://localhost:8080/px/applyServer", service);

        System.out.println("webservice发布成功!");

   }

   /**

    * 使用axis调用webservice(注:需要先发布webservice,方法见博客地址:

https://blog.csdn.net/Diana_weiwei/article/details/81531285

    * @param ele

    */

   public void test2(){

        Service service = new Service(); 

      String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><userList>";

        Call call;

     try {

          call = (Call) service.createCall();

          call.setTargetEndpointAddress("http://localhost:8080/px/webservice/applyService?wsdl"); 

           call.setOperationName(new QName("http://service.train.px.com/","applyUserList")); 

           call.setUseSOAPAction(true); 

           //这下面两行一定要加上,否则接收在服务器端收不到。 

           call.addParameter("xmlStr", XMLType.XSD_STRING, ParameterMode.IN); 

           call.setReturnType(XMLType.XSD_STRING); 

           Object[] c=new Object[]{xmlStr};

           String result = (String) call.invoke(c); 

           System.out.println(result); 

          

         //将返回的字符串转换成list集合 

         //JSONArray array = JSONArray.fromObject(result); 

         //List<Album> list = JSONArray.toList(array,Album.class);

     } catch (ServiceException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

     } catch (RemoteException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

     } 

    }

   /**

    * 使用dom4j封装返回结果

    * @param userList

    * @return

    */

   public String test3(List<TrainClassStudent> userList) {

     // TODO Auto-generated method stub

     Document document = DocumentHelper.createDocument();

        //添加以根节点

        Element root = document.addElement("userList");

 

        for (TrainClassStudent user : userList) {

            Element area = root.addElement("user");

            area.addElement("id").setText(StringUtils.isEmpty(user.getUserId())?"":user.getUserId());

            area.addElement("name").setText(StringUtils.isEmpty(user.getName())?"":user.getName());

            area.addElement("code").setText(StringUtils.isEmpty(user.getJobNumber())?"":user.getJobNumber());

            area.addElement("orgId").setText(StringUtils.isEmpty(user.getOrgId())?"":user.getOrgId());

            area.addElement("sex").setText(user.getSex()==null?"":user.getSex().toString());

            area.addElement("phone").setText(StringUtils.isEmpty(user.getStuTel())?"":user.getStuTel());

            area.addElement("email").setText(StringUtils.isEmpty(user.getEmail())?"":user.getEmail());

            area.addElement("certiCode ").setText(StringUtils.isEmpty(user.getCertificateId())?"":user.getCertificateId());

            area.addElement("ifTrain  ").setText("true");

            area.addElement("trainTime  ").setText(user.getRealEndDate()==null?"":user.getRealEndDate().toString());

        }

 

        return document.asXML();

   }

     /**

      * 使用dom4j解析返回结果xml

      * @param ele

      * @throws DocumentException

      */

    private static void test4() throws DocumentException {

     // TODO Auto-generated method stub

      String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><userList>"

             + "<user><id></id><name>白玉红</name><code> </code><orgId></orgId><sex></sex><phone>15235584422</phone><email></email><certiCode >510101199303280074</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Fri Apr 13 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>张大林</name><code> </code><orgId></orgId><sex></sex><phone>18552452522</phone><email></email><certiCode >510101199303280090</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Fri Apr 13 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>总公司管理员</name><code>201</code><orgId></orgId><sex></sex><phone></phone><email></email><certiCode >421281199001236314</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sat Mar 31 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>张涛</name><code>203</code><orgId></orgId><sex></sex><phone>13725424111</phone><email></email><certiCode >510101199303270079</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sat Mar 31 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>小明</name><code> </code><orgId></orgId><sex></sex><phone>181123456789</phone><email></email><certiCode >510101199303270175</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sun Apr 01 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>小红</name><code>    </code><orgId></orgId><sex></sex><phone>19822584522</phone><email></email><certiCode >421281199001236314</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sun Apr 01 00:00:00 CST 2018</trainTime  ></user>"

             + "</userList>";

     Document doc = DocumentHelper.parseText(xmlStr);

    /**
             * node与element的区别
             * ode是节点,一个属性、一段文字、一个注释等都是节点,而Element是元素,是比较完整的一个xml的元素,
             * 即我们口头上说的xml“结点”(此处故意使用“结”字,以示与“节点”Node区别),我觉得这点和HTNL中DOM很像,
             * 比如说<div id="ss"></div>其中它由元素节点、属性节点和文本节点组成,但是它是一个div元素。
             * 总结:元素是元素节点,是节点中的一种,但元素节点中可以包含很多的节点。
             * 我们平时在开发中经常大都使用的是Element,我们怎样把Node转为Element呢,
             *   Element sosaleInOtherHeadEle = (Element) sosaleInOtherELe.selectSingleNode("sosaleInOtherHead");
             */

     Element root = (Element) doc.getRootElement();

     System.out.println( root.getName()+":"+root.getText().trim());

     //Users根节点开始遍历,像【属性=值】的形式存为一个Attribute对象存储在List集合中

     List<Attribute> attrList = root.attributes();

     for(Attribute attr : attrList){

        //每循环一次,解析此节点的一个【属性=值】,没有则输出空

        String name = attr.getName();

        String value = attr.getValue();

        System.out.println(name+"="+value);

     }

    

     List<Element> eleList = root.elements();

     //递归遍历父节点下的所有子节点

     for(Element e : eleList){

        System.out.println(e.getName()+":"+e.getText().trim());

        List<Element> sbean = e.elements();

        String bean="";

        for(Element eb : sbean){

          bean+=eb.getName()+":"+eb.getText().trim();

        }

        System.out.println(bean);

     }

   }

}

package com.px.train.util;

 

import java.rmi.RemoteException;

import java.util.List;

 

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import javax.xml.ws.Endpoint;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.XMLType;

import org.apache.commons.lang3.StringUtils;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

 

import com.px.train.model.TrainClassStudent;

import com.px.train.service.impl.ApplyPostServiceImpl;

 

public class MyServer {

 

   public static void main(String[] args) {

     test1();

    }

   /**

    * webservice发布测试方法

    * @param ele

    */

   private static void test1() {

     // TODO Auto-generated method stub

     ApplyPostServiceImpl service=new ApplyPostServiceImpl();

     System.out.println("star");

     String str="",uri="";

        String result=service.applyUserList(uri,str);

        System.out.println(result);

        Endpoint.publish("http://localhost:8080/px/applyServer", service);

        System.out.println("webservice发布成功!");

   }

   /**

    * 使用axis调用webservice(注:需要先发布webservice,方法见博客地址:)

    * @param ele

    */

   public void test2(){

        Service service = new Service(); 

      String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><userList>";

        Call call;

     try {

          call = (Call) service.createCall();

          call.setTargetEndpointAddress("http://localhost:8080/px/webservice/applyService?wsdl"); 

           call.setOperationName(new QName("http://service.train.px.com/","applyUserList")); 

           call.setUseSOAPAction(true); 

           //这下面两行一定要加上,否则接收在服务器端收不到。 

           call.addParameter("xmlStr", XMLType.XSD_STRING, ParameterMode.IN); 

           call.setReturnType(XMLType.XSD_STRING); 

           Object[] c=new Object[]{xmlStr};

           String result = (String) call.invoke(c); 

           System.out.println(result); 

          

         //将返回的字符串转换成list集合 

         //JSONArray array = JSONArray.fromObject(result); 

         //List<Album> list = JSONArray.toList(array,Album.class);

     } catch (ServiceException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

     } catch (RemoteException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

     } 

    }

   /**

    * 使用dom4j封装返回结果

    * @param userList

    * @return

    */

   public String test3(List<TrainClassStudent> userList) {

     // TODO Auto-generated method stub

     Document document = DocumentHelper.createDocument();

        //添加以根节点

        Element root = document.addElement("userList");

 

        for (TrainClassStudent user : userList) {

            Element area = root.addElement("user");

            area.addElement("id").setText(StringUtils.isEmpty(user.getUserId())?"":user.getUserId());

            area.addElement("name").setText(StringUtils.isEmpty(user.getName())?"":user.getName());

            area.addElement("code").setText(StringUtils.isEmpty(user.getJobNumber())?"":user.getJobNumber());

            area.addElement("orgId").setText(StringUtils.isEmpty(user.getOrgId())?"":user.getOrgId());

            area.addElement("sex").setText(user.getSex()==null?"":user.getSex().toString());

            area.addElement("phone").setText(StringUtils.isEmpty(user.getStuTel())?"":user.getStuTel());

            area.addElement("email").setText(StringUtils.isEmpty(user.getEmail())?"":user.getEmail());

            area.addElement("certiCode ").setText(StringUtils.isEmpty(user.getCertificateId())?"":user.getCertificateId());

            area.addElement("ifTrain  ").setText("true");

            area.addElement("trainTime  ").setText(user.getRealEndDate()==null?"":user.getRealEndDate().toString());

        }

 

        return document.asXML();

   }

     /**

      * 使用dom4j解析返回结果xml

      * @param ele

      * @throws DocumentException

      */

    private static void test4() throws DocumentException {

     // TODO Auto-generated method stub

      String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><userList>"

             + "<user><id></id><name>白玉红</name><code> </code><orgId></orgId><sex></sex><phone>15235584422</phone><email></email><certiCode >510101199303280074</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Fri Apr 13 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>张大林</name><code> </code><orgId></orgId><sex></sex><phone>18552452522</phone><email></email><certiCode >510101199303280090</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Fri Apr 13 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>总公司管理员</name><code>201</code><orgId></orgId><sex></sex><phone></phone><email></email><certiCode >421281199001236314</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sat Mar 31 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>张涛</name><code>203</code><orgId></orgId><sex></sex><phone>13725424111</phone><email></email><certiCode >510101199303270079</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sat Mar 31 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>小明</name><code> </code><orgId></orgId><sex></sex><phone>181123456789</phone><email></email><certiCode >510101199303270175</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sun Apr 01 00:00:00 CST 2018</trainTime  ></user>"

             + "<user><id></id><name>小红</name><code>    </code><orgId></orgId><sex></sex><phone>19822584522</phone><email></email><certiCode >421281199001236314</certiCode ><ifTrain  >true</ifTrain  ><trainTime  >Sun Apr 01 00:00:00 CST 2018</trainTime  ></user>"

             + "</userList>";

     Document doc = DocumentHelper.parseText(xmlStr);

     Element root = (Element) doc.getRootElement();

     System.out.println( root.getName()+":"+root.getText().trim());

     //Users根节点开始遍历,像【属性=值】的形式存为一个Attribute对象存储在List集合中

     List<Attribute> attrList = root.attributes();

     for(Attribute attr : attrList){

        //每循环一次,解析此节点的一个【属性=值】,没有则输出空

        String name = attr.getName();

        String value = attr.getValue();

        System.out.println(name+"="+value);

     }

    

     List<Element> eleList = root.elements();

     //递归遍历父节点下的所有子节点

     for(Element e : eleList){

        System.out.println(e.getName()+":"+e.getText().trim());

        List<Element> sbean = e.elements();

        String bean="";

        for(Element eb : sbean){

          bean+=eb.getName()+":"+eb.getText().trim();

        }

        System.out.println(bean);

     }

   }

}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值