1 public static void main(String[] args) throws Exception { 2 XnServiceGetUserRequest xnServiceGetUserRequest = new XnServiceGetUserRequest(); 3 4 xnServiceGetUserRequest.setOPTYPE("www"); 5 xnServiceGetUserRequest.setPERNR("eeeeeeee"); 6 xnServiceGetUserRequest.setToken("yyyyyyy"); 7 8 // 对象转xml 9 System.out.println(XmlUtils.xmlMapper.writeValueAsString(xnServiceGetUserRequest)); 10 11 String xmlStr = "<XnServiceGetUserRequest><Token>yyyyyyy</Token><PERNR>eeeeeeee</PERNR><OPTYPE>www</OPTYPE></XnServiceGetUserRequest>"; 12 // xml转对象 13 XnServiceGetUserRequest xntest = XmlUtils.xmlMapper.readValue(xmlStr, XnServiceGetUserRequest.class); 14 15 // 打印是否转对象成功 16 System.out.println(XmlUtils.xmlMapper.writeValueAsString(xntest)); 17 18 }
package com.cigna.hmc.groupinsurance.utils; import com.fasterxml.jackson.dataformat.xml.XmlMapper; /** * * @desc 采用fastxml 不使用老的codehaus * @author josnow * @date 2017年11月23日 下午5:01:44 * @version 1.0.0 */ public class XmlUtils { public static final XmlMapper xmlMapper = new XmlMapper(); }