• Java中xml转javaBean


    Java中xml转javaBean

    maven坐标

            <dependency>
                <groupId>com.fasterxml.jackson.dataformatgroupId>
                <artifactId>jackson-dataformat-xmlartifactId>
                 <version>2.13.4version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    代码测试

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    输出内容

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    【JAVASE系列】06_继承,多态与final
    图形编辑器基于Paper.js教程01:入门必读
    Linux常用指令--时间操作指令
    c++ -const和指针的关系 --每日学习八股文
    【mq】从零开始实现 mq-08-配置优化 fluent
    人工智能的未来以及人工智能如何影响教育、医学、营销领域
    conda安装与镜像源配置
    问卷调查小程序功能清单
    常用胶水介绍
    halcon实时采集数据
  • 原文地址:https://blog.csdn.net/weixin_44485508/article/details/132780022