Java中xml转javaBean

Java中xml转javaBean

maven坐标

xml 复制代码
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
             <version>2.13.4</version>
        </dependency>

代码测试

java 复制代码
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="<EventNotificationAlert version=\"2.0\" xmlns=\"http://www.hikvision.com/ver20/XMLSchema\">\r\n" +
                "<ipAddress>192.168.1.228</ipAddress>\r\n" +
                "<ipv6Address>::</ipv6Address>\r\n" +
                "<portNo>80</portNo>\r\n" +
                "<protocol>HTTP</protocol>\n" +
                "<macAddress>08:54:11:a1:98:aa</macAddress>\r\n" +
                "<channelID>1</channelID>\r\n" +
                "<dateTime>2023-09-08T15:49:45+08:00</dateTime>\r\n" +
                "<activePostCount>1</activePostCount>\r\n" +
                "<eventType>videoloss</eventType>\r\n" +
                "<eventState>inactive</eventState>\r\n" +
                "<eventDescription>videoloss alarm</eventDescription>\r\n" +
                "<channelName>测试001</channelName>\r\n" +
                "</EventNotificationAlert>";
        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
相关推荐
跟着珅聪学java29 分钟前
spring boot +Elment UI 上传文件教程
java·spring boot·后端·ui·elementui·vue
我命由我1234534 分钟前
Spring Boot 自定义日志打印(日志级别、logback-spring.xml 文件、自定义日志打印解读)
java·开发语言·jvm·spring boot·spring·java-ee·logback
lilye6635 分钟前
程序化广告行业(55/89):DMP与DSP对接及数据统计原理剖析
java·服务器·前端
徐小黑ACG1 小时前
GO语言 使用protobuf
开发语言·后端·golang·protobuf
0白露3 小时前
Apifox Helper 与 Swagger3 区别
开发语言
Tanecious.3 小时前
机器视觉--python基础语法
开发语言·python
叠叠乐4 小时前
rust Send Sync 以及对象安全和对象不安全
开发语言·安全·rust
战族狼魂4 小时前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
Tttian6225 小时前
Python办公自动化(3)对Excel的操作
开发语言·python·excel
xyliiiiiL5 小时前
ZGC初步了解
java·jvm·算法