相关文章推荐
傻傻的地瓜  ·  C# | ...·  1 周前    · 
鬼畜的钢笔  ·  docker mysqldump ...·  9 月前    · 
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.9.5</version> </dependency>

@JacksonXmlRootElement:指定生成xml根标签的名字

属性: namespace,localName

@JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名

属性: namespace,localName,isAttribute(default: false)

@JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名

属性: namespace,localName,useWrapping (default:true)

@JacksonXmlText:指定当前这个值,没有xml标签包裹。

属性: namespace,localName

  • 实体类转XML
  • import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
    import lombok.Data;
    import java.io.Serializable;
     * @Author by peng .
     * @Descriptions
     * @Datetime in 2018/5/18 08:05.
    @Data
    @JacksonXmlRootElement(localName = "request")
    public class RequestXml<T> implements Serializable {
        private static final long serialVersionUID = 1L;
        private String request;
        // isAtrribute 设为true时,该字段为根标签的属性
        @JacksonXmlProperty(isAttribute = true)
        private String service;
        @JacksonXmlProperty(isAttribute = true)
        private String method;
        @JacksonXmlProperty
        private String token;
        @JacksonXmlProperty
        private T content;
    复制代码

    工具类

    public static String objectToXml(Object object) throws IOException {
        xmlMapper.setDefaultUseWrapper(false);
        /* 字段为null,自动忽略,不再序列化 */
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        /* 设置转换模式 */
        xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
        String resultXml = xmlMapper.writeValueAsString(object);
        return resultXml;
    复制代码

    测试

    public static void test2() throws IOException {
        RequestXml<Dept> requestXml = new RequestXml<>();
        Dept dept = new Dept();
        dept.setDeptCode("12345");
        dept.setDeptName("开发部");
        requestXml.setToken("adjfa;fjlaksdjfasdfiew");
        requestXml.setContent(dept);
        requestXml.setService("DeptService");
        requestXml.setMethod("getDeptList");
        String xml = JsonUtil.objectToXml(requestXml);
        log.info(xml);
    复制代码

    输出

    <request service="DeptService" method="getDeptList">
        <token>adjfa;fjlaksdjfasdfiew</token>
        <content>
        	<deptCode>12345</deptCode>
        	<deptName>开发部</deptName>
        </content>
    </request>
    
  • xml转实体类
  •  public static <T> T xmlToObject(String xml,Class<T> clazz) throws IOException, XMLStreamException {
        xmlMapper.setDefaultUseWrapper(false);
        /* 字段为null,自动忽略,不再序列化 */
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        /* 设置转换模式 */
        xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
        return xmlMapper.readValue(xml, clazz);
    复制代码

    实体类

    @Data
    @JacksonXmlRootElement(localName = "response")
    public class ResponseData {
        @JacksonXmlProperty
        private String errorCode;
        @JacksonXmlProperty
        private String errorDesc;
        @JacksonXmlProperty
        private ResponseResult result;
    @Data
    @JacksonXmlRootElement(localName = "result")
    public class ResponseResult {
        @JacksonXmlProperty(localName = "lbsLocRecord")
        private List<LbsLocRecord> lbsLocRecord;
    @Data
    @JacksonXmlRootElement(localName = "lbsLocRecord")
    @EqualsAndHashCode(callSuper = false)
    @TableName("lbs_loc_record")
    public class LbsLocRecord extends BaseEntity {
        @TableId(value = "locate_record_id", type = IdType.ID_WORKER)
        private Long locateRecordId;
        /** 终端ID */
        private String deviceId;
        /** 偏转再加密的经度坐标值 */
        private String jmx;
        /** 偏转再加密的纬度坐标值 */
        private String jmy;
        /** 经度坐标值 */
        private String lon;
        /** 纬度坐标值 */
        private String lat;
        /** 定位时间 ,格式:YYYY-MM-DD HH:MI:SS */
        private String time;
        /** 位置描述 */
        private String position;
        /** 错误编码 */
        @TableField(exist = false)
        private String errcode;
        /** 错误信息 */
        @TableField(exist = false)
        private String errmsg;
        /** 错误信息 */
        @TableField(exist = false)
        private boolean timeOut;
    复制代码

    测试类

     public static void testXmlToObject() throws IOException, XMLStreamException {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>\n" +
                "  <errorCode>00000</errorCode>\n" +
                "  <errorDesc></errorDesc>\n" +
                "  <result>\n" +
                "    <lbsLocRecord>\n" +
                "      <deviceId>15054916000</deviceId>\n" +
                "      <jmx>0</jmx>\n" +
                "      <jmy>0</jmy>\n" +
                "      <lon>127.16732</lon>\n" +
                "      <lat>44.93510</lat>\n" +
                "      <time>2018-05-18 14:06:03</time>\n" +
                "      <position>黑龙江省哈尔滨市五常市金山公园五常镇金山屯;G1211/S222/亚臣路;金山大街/G1211(路口);金山水保科技园附近</position>\n" +
                "      <timeOut>false</timeOut>\n" +
                "    </lbsLocRecord>\n" +
                "    <lbsLocRecord>\n" +
                "      <deviceId>15244366000</deviceId>\n" +
                "      <jmx>0</jmx>\n" +
                "      <jmy>0</jmy>\n" +
                "      <lon>0.0</lon>\n" +
                "      <lat>0.0</lat>\n" +
                "      <errcode>9990240</errcode>\n" +
                "      <errmsg>LBMP_CALLEE_NO_EXCEPTION</errmsg>\n" +
                "      <time>2018-05-18 14:25:58</time>\n" +
                "      <position></position>\n" +
                "      <timeOut>false</timeOut>\n" +
                "    </lbsLocRecord>\n" +
                "  </result>\n" +
                "</root>\n";
        ResponseData responseData = JsonUtil.xmlToObject(xml, ResponseData.class);
        String errorCode = responseData.getErrorCode();
        if ("00000".equals(errorCode)) {
            List<LbsLocRecord> resultList = responseData.getResult().getLbsLocRecord();
            for (LbsLocRecord record : resultList){
                if (StringUtils.isBlank(record.getErrcode())){
                    log.info("插入数据");
        String string = JSON.toJSONString(responseData);
        log.info("responseString:"+ string );
        log.info("responseData:"+ JSON.parse(string));
        /*String string = JsonUtil.xmlToJson(xml);
        log.info("string:"+ string);
        log.info("object:"+ JSON.parse(string));*/
          安装掘金浏览器插件