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
相关推荐
捕鲸叉25 分钟前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer30 分钟前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq32 分钟前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
Yaml41 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~1 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616881 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
aloha_7891 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
记录成长java2 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
前端青山2 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
睡觉谁叫~~~2 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust