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 = "
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
} 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
// 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 = "
+ "
+ "
+ "201
+ "203
+ "
+ "
+ "";
Document doc = DocumentHelper.parseText(xmlStr);
/**
* node与element的区别
* ode是节点,一个属性、一段文字、一个注释等都是节点,而Element是元素,是比较完整的一个xml的元素,
* 即我们口头上说的xml“结点”(此处故意使用“结”字,以示与“节点”Node区别),我觉得这点和HTNL中DOM很像,
* 比如说
Element root = (Element) doc.getRootElement();
System.out.println( root.getName()+":"+root.getText().trim());
//从Users根节点开始遍历,像【属性=值】的形式存为一个Attribute对象存储在List集合中
List
for(Attribute attr : attrList){
//每循环一次,解析此节点的一个【属性=值】,没有则输出空
String name = attr.getName();
String value = attr.getValue();
System.out.println(name+"="+value);
}
List
//递归遍历父节点下的所有子节点
for(Element e : eleList){
System.out.println(e.getName()+":"+e.getText().trim());
List
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 = "
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
} 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
// 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 = "
+ "
+ "
+ "201
+ "203
+ "
+ "
+ "";
Document doc = DocumentHelper.parseText(xmlStr);
Element root = (Element) doc.getRootElement();
System.out.println( root.getName()+":"+root.getText().trim());
//从Users根节点开始遍历,像【属性=值】的形式存为一个Attribute对象存储在List集合中
List
for(Attribute attr : attrList){
//每循环一次,解析此节点的一个【属性=值】,没有则输出空
String name = attr.getName();
String value = attr.getValue();
System.out.println(name+"="+value);
}
List
//递归遍历父节点下的所有子节点
for(Element e : eleList){
System.out.println(e.getName()+":"+e.getText().trim());
List
String bean="";
for(Element eb : sbean){
bean+=eb.getName()+":"+eb.getText().trim();
}
System.out.println(bean);
}
}
}