xml 报文的组装:
1.使用String.format通过占位符拼接;(采用这种,优势简化代码)
2.使用目前项目中得方法,使用ftl模板 算了没看懂
String strRequestXML = ""; try { //将DTO转换成xml Map<String, Object> packet = new HashMap<String, Object>(); packet.put("packet",packetDto); FreemarkerUtil.initTemplate(templateFile); // 获取模板 strRequestXML = FreemarkerUtil.returnXML(packet); // 返回接口使用的参数 } catch (Exception e) { e.printStackTrace(); }
xml的解析目前想到的方法:
1,写个截取字符串的方法,以前在别的项目中看到过
思路就是 入参为节点 找到 两个位置 ,然后截取
2.使用目前项目解析方式 XStream是一个Java对象和XML相互转换的工具 麻烦需要配置 xml对应的解析类
思路详情:使用XStream实现Java对象与XML互相转换 segmentfault 思否
responseStr = SendPropertyDoPost(packetDto,sendType); Body body = new Body(); Head head = new Head(); Object objbody = null; Object objhead = null; objhead = ParserXml.getHead(responseStr); head = (Head)objhead; 1.stinrg 转 body public static Object getBody(String s) { // String s = new SendCarInfoToCore().SendCarInfoDoPost(); String xmlString = deleteXMLTag(s); try { Packet packet = toBean(xmlString, Packet.class); return packet.getBody(); } catch (Exception e) { ErrRoot errRoot = toBean(xmlString, ErrRoot.class); e.printStackTrace(); return errRoot.getRetMsg(); } }
2.Packet.java
package com.sinosoft.b2c.propertyInsurance.agriculture.Dto; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Packet") public class Packet { @XStreamAlias("Body") private Body body; @XStreamAlias("Head") private Head head; public Body getBody() { return body; } public void setBody(Body body) { this.body = body; } public Head getHead() { return head; } public void setHead(Head head) { this.head = head; } }
3.Body.java
package com.sinosoft.b2c.propertyInsurance.agriculture.Dto; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Body") public class Body { @XStreamAlias("OperInfo") private OperInfoDto operInfo; @XStreamAlias("ClaimInfoList") private List<TaskDto> taskList; @XStreamAlias("ImageInfoList") private List<ImageInfo> imageInfoList; public List<ImageInfo> getImageInfoList() { return imageInfoList; } public void setImageInfoList(List<ImageInfo> imageInfoList) { this.imageInfoList = imageInfoList; } public List<TaskDto> getTaskList() { return taskList; } public void setTaskList(List<TaskDto> taskList) { this.taskList = taskList; } public OperInfoDto getOperInfo() { return operInfo; } public void setOperInfo(OperInfoDto operInfo) { this.operInfo = operInfo; } }
3.百度 J-A-X-B