在Java开发中经常会遇到使用XML文件传数据的情况,因此就要求我们必须学会如何解析XML文件,现在我们系统里经常使用的DOM4j和XStream两种方式进行解析XML文件的。现在就第二种方法,总结一下我的使用感受和经验。首先是需要两个操作XStream的jar包,我们用的是xpp3_min-1.1.3.4.O.jar和xstream-1.3.1.jar(我已经上传我的CSDN资源啦,欢迎免费下载)现在就我遇到的这个功能做下总结:
对于请求的参数的处理,首先我们需要在定义相关的JavaBean,然后将Bean转化成XML格式的字符串,然后写入流OutputStream,通过流的形式传给服务器,服务器响应之后又以XML的形式返回响应数据。
因此针对请求参数,需要定义一下实体类(get,set方法省略):
public class TongChengWangMessage { private Trasinf trasinf; private Ucom ucom; } public class Trasinf { private String service; // 服务器名称 private String act; // 分配给各分公司的账号 } public class Ucom { private String startTime; private String endTime; private String policyCode; private String isInsure; }
1.请求参数由JavaBean转化成XML形式,需要用到Stream的toXML()方法:
TongChengWangMessage message = new TongChengWangMessage(); Trasinf trasif = new Trasinf(); trasif.setAct("1111"); trasif.setService("GetInsuranceInfo"); Ucom ucom = new Ucom(); ucom.setPolicyCode("tryyu"); ucom.setIsInsure("1"); ucom.setStartTime("20130529"); ucom.setEndTime("20130530"); message.setTrasinf(trasif); message.setUcom(ucom); String res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; XStream xx = new XStream(); //不加下面这一句,会出现节点 com.cpic.ryx.bean.commpay.TongChengWangMessage xx.alias("reqinfo",TongChengWangMessage.class); System.out.println("send:\n"+res + xx.toXML(message));
输出为: send: <?xml version="1.0" encoding="UTF-8"?> <reqinfo> <trasinf> <service>GetInsuranceInfo</service> <act>1111</act> </trasinf> <ucom> <startTime>20130529</startTime> <endTime>20130530</endTime> <policyCode>tryyu</policyCode> <isInsure>1</isInsure> </ucom> </reqinfo>
注意为了让节点保持简洁,要对最外层节点加上别名 xx.alias("reqinfo",TongChengWangMessage.class);不加就会出现节点<reqinfo>
变成<com.cpic.ryx.bean.commpay.TongChengWangMessage>
2.响应返回的XML文件流,转化成JavaBean
通过上面的图片看到响应的XML文件格式,因此需要定义相应的实体类:
public class XmlResponseBeanToTongCheng { private String st; private TongchengInsuranceList insuranceList; } public class TongchengInsuranceList { private String Count; private String TotalPremium; private ArrayList<TongChengInsurance> Details; }
public class TongChengInsurance { private String tcSerialNo; private String updated; private String applicantName; private String policyNum; private String status; private String planCode; private String premium; public TongChengInsurance(){ super(); } ..... }
注意:对应循环出现的节点一般放到List中处理!
得到响应的返回结果后,需要用XStream的fromXML()方法解析得到JavaBean.
XStream xxq = new XStream(); //接受返回 java.io.InputStream in = httpurlconnection.getInputStream(); //要加下面的,否则会报错 com.thoughtworks.xstream.mapper.CannotResolveClassException: rlt : rlt //涉及自定义类的情况都需要alias xxq.alias("rlt",XmlResponseBeanToTongCheng.class); xxq.alias("Insurance",TongChengInsurance.class); Reader reader=new BufferedReader(new InputStreamReader(in)); XmlResponseBeanToTongCheng message2=(XmlResponseBeanToTongCheng) xxq.fromXML(reader); in.close(); reader.close(); System.out.println("返回结果:\n"+xxq.toXML(message2));
得到的结果如下:
返回结果: <rlt> <st>1</st> <insuranceList> <Count>25</Count> <TotalPremium>5041.0</TotalPremium> <Details> <Insurance> <tcSerialNo>流水号1</tcSerialNo> <updated>20130529</updated> <applicantName>姓名1</applicantName> <policyNum>190471316349818</policyNum> <status>10</status> <planCode>EA360114</planCode> <premium>53.0</premium> </Insurance> <Insurance> <tcSerialNo>流水号2</tcSerialNo> <updated>20130529</updated> <applicantName>姓名2</applicantName> <policyNum>190471316349830</policyNum> <status>10</status> <planCode>EA361101</planCode> <premium>612.0</premium> </Insurance> </Details> </insuranceList> </rlt>