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);
}
}
}