Java中xml转javaBean

Java中xml转javaBean

maven坐标

        <dependency>
            <groupId>com.fasterxml.jackson.dataformatgroupId>
            <artifactId>jackson-dataformat-xmlartifactId>
             <version>2.13.4version>
        dependency>

代码测试

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import javax.xml.bind.annotation.XmlRootElement;
import java.time.LocalDateTime;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

/**
 * Author:daWang
 * Date:2023/9/9  16:38
 */
public class XmlTest {

    public static void main(String[] args) throws Exception{
        String str="\r\n" +
                "192.168.1.228\r\n" +
                "::\r\n" +
                "80\r\n" +
                "HTTP\n" +
                "08:54:11:a1:98:aa\r\n" +
                "1\r\n" +
                "2023-09-08T15:49:45+08:00\r\n" +
                "1\r\n" +
                "videoloss\r\n" +
                "inactive\r\n" +
                "videoloss alarm\r\n" +
                "测试001\r\n" +
                "";
        ObjectMapper objectMapper = new XmlMapper();

        EventXml eventXml = objectMapper.readValue(str, EventXml.class);

        System.out.println(JSONUtil.toJsonStr(eventXml));

    }



    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class EventXml{

        private String ipAddress;
        private String ipv6Address;
        private Integer portNo;
        private String protocol;
        private String macAddress;
        private Integer channelID;
        private String dateTime;
        private Integer activePostCount;
        private String eventType;
        private String eventState;
        private String eventDescription;
        private String channelName;
    }
}

输出内容

Connected to the target VM, address: '127.0.0.1:62168', transport: 'socket'
{"ipAddress":"192.168.1.228","ipv6Address":"::","portNo":80,"protocol":"HTTP","macAddress":"08:54:11:a1:98:aa","channelID":1,"dateTime":"2023-09-08T15:49:45+08:00","activePostCount":1,"eventType":"videoloss","eventState":"inactive","eventDescription":"videoloss alarm","channelName":"测试001"}
Disconnected from the target VM, address: '127.0.0.1:62168', transport: 'socket'

Process finished with exit code 0

你可能感兴趣的:(java,xml,开发语言)