1.学会使用xml的各个注解与含义,参考网址如下http://www.jb51.cc/article/p-fnvyrxrz-zm.html
举例:
</pre><pre name="code" class="java">@XmlRootElement(name="RequestOrder") @XmlType(propOrder={"clientFlag","orderNo","subOrderNo","tradeNo","mailNo","type","flag","sender","receiver","sendStartTime","sendEndTime","codAmount","items","insuranceValue","loadRequire","remark","dataFlag"}) @XmlAccessorType(XmlAccessType.FIELD) public class OrderRequest { /**客户标示**/ @XmlElement(name="logisticProviderID") private String clientFlag; /** 客户单号**/ private String orderNo; /** 子订单业号**/ private String subOrderNo; /** 业务交易号**/ private String tradeNo; /** 快递单号**/ private String mailNo; /** 类型 代收订单0 普通正向 1 退货订单 2 **/ private Integer type; /** 订单标示 0 **/ private Integer flag; /** 寄件人 **/ private PersonInfo sender; /** 收件人 **/ private PersonInfo receiver; /** 物流上门取货时间段 **/ private String sendStartTime; private String sendEndTime; /** 代收金额**/ private double codAmount; /** 商品信息 **/ @XmlElementWrapper(name="items") @XmlElement(name="item") private List<ZJSOrderItems> items; /** 保险费**/ private double insuranceValue; /** 配载要求:无/禁航/禁航铁 **/ private String loadRequire; /** 重要提示 **/ private String remark; /** 数据标示 (用于分仓发货,C2C类客户省略) **/ private String dataFlag; //构造方法 //getter与setter方法 }
2.编写JaxUtil类,方便进行xmlTobean与beanToXml的转化
public class JaxbUtil { /** The logger. */ private static final Log LOGGER = LogFactory.getLog(JaxbUtil.class); /** * JavaBean转换成xml 默认编码UTF-8 * * @param obj * @param writer * @return */ public static String convertToXml(Object obj) { return convertToXml(obj,"UTF-8"); } /** * JavaBean转换成xml * * @param obj * @param encoding * @return */ public static String convertToXml(Object obj,String encoding) { String result = null; try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT,Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_ENCODING,encoding); StringWriter writer = new StringWriter(); marshaller.marshal(obj,writer); String xmlTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; result = xmlTitle + writer.toString(); } catch (Exception e) { LOGGER.error("convertToXml has error,",e); } return result; } /** * xml转换成JavaBean * * @param xml * @param c * @return */ @SuppressWarnings("unchecked") public static <T> T converyToJavaBean(String xml,Class<T> c) { T t = null; try { JAXBContext context = JAXBContext.newInstance(c); Unmarshaller unmarshaller = context.createUnmarshaller(); <span style="font-family: Arial,Helvetica,sans-serif; background-color: rgb(255,255,255);"></span><pre code_snippet_id="1656191" snippet_file_name="blog_20160421_2_9698971" name="code" class="java"><span style="font-family: Arial,255);"><span style="white-space:pre"> </span>xml=xml.replaceAll("<","<");</span>xml=xml.replaceAll(">",">"); xml=xml.replace("<string xmlns=\"http://edi.zjs.com.cn/\"><?xml version=\"1.0\" encoding=\"utf-8\" ?>","");
xml=responseData.replace("</string>","");
<span style="white-space:pre"> </span>t = (T) unmarshaller.unmarshal(new StringReader(xml)); } catch (Exception e) { LOGGER.error("converyToJavaBean has error,e); } return t; } }