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
相关推荐
时光の尘5 分钟前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
paopaokaka_luck6 分钟前
[371]基于springboot的高校实习管理系统
java·spring boot·后端
以后不吃煲仔饭18 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师19 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
前端拾光者23 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
The_Ticker25 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
程序猿阿伟25 分钟前
《C++ 实现区块链:区块时间戳的存储与验证机制解析》
开发语言·c++·区块链
傻啦嘿哟43 分钟前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
大数据编程之光1 小时前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
初九之潜龙勿用1 小时前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net