Jackson XML

发布于:2024-05-22 ⋅ 阅读:(170) ⋅ 点赞:(0)

1 添加依赖

<dependency>
	<groupId>com.fasterxml.jackson.dataformat</groupId>
	<artifactId>jackson-dataformat-xml</artifactId>
	<version>2.11.3</version>
</dependency>
package com.xu.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;

import java.io.Serializable;

@Data
@JacksonXmlRootElement(localName = "Data")
public class XmlEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("Id")
    private String id;

    @JsonProperty("Name")
    private String name;

    @JsonProperty("Body")
    private Object body;

}
package com.xu.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class XmlData implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("Code")
    private int code = 1;

    @JsonProperty("Time")
    private long name = new Date().getTime();

}

2 XML转对象

package com.xu;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.xu.entity.XmlData;
import com.xu.entity.XmlEntity;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UnitTest {

    @Test
    public void xml2bean() throws Exception {
        String xml = "<?xml version='1.0' encoding='UTF-8'?><Data><Id>1</Id><Name>test</Name><Body><Code>1</Code><Time>1715755993269</Time></Body></Data>";
        XmlMapper mapper = new XmlMapper();
        XmlEntity entity = mapper.readValue(xml, XmlEntity.class);
        System.out.println(JSONUtil.toJsonPrettyStr(entity));
    }

}

3 对象转XML

package com.xu;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.xu.entity.XmlData;
import com.xu.entity.XmlEntity;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UnitTest {

    @Test
    public void bean2xml() throws Exception {
        XmlEntity test = new XmlEntity();
        test.setId("1");
        test.setName("test");
        test.setBody(new XmlData());

        XmlMapper mapper = new XmlMapper();
        mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
        String xml = mapper.writeValueAsString(test);
        System.out.println(xml);
    }

}

4 根据路径读取

package com.xu;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.xu.entity.XmlData;
import com.xu.entity.XmlEntity;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UnitTest {

    @Test
    public void getByPath() throws Exception {
        String xml = "<?xml version='1.0' encoding='UTF-8'?><Data><Id>1</Id><Name>test</Name><Body><Code>1</Code><Time>1715755993269</Time></Body></Data>";
        XmlMapper mapper = new XmlMapper();
        JsonNode node = mapper.readTree(xml);
        JsonNode code = node.at("/Name");
        System.out.println(code.asText());
    }

}


网站公告

今日签到

点亮在社区的每一天
去签到