Jackson 除了可以处理 JSON,还可以用来处理 XML(jackson-dataformat-xml 模块),可以轻松完成 Java 对象和 XML 文档的互转;本文主要介绍使用 Jackson 来处理 XML,文中所使用到的软件版本:Java 1.8.0_321、Jackson 2.13.3。
1、简介
jackson-dataformat-xml 模拟 JAXB “代码优先” 的数据绑定方式,提供低级以及高级的方法来完成数据绑定工作。值得注意的是,jackson-dataformat-xml 的目标不是完整的 JAXB 克隆,或者成为一个通用的 XML 工具包。具体来说:
- While XML serialization should ideally be similar to JAXB output, deviations are not automatically considered flaws (there are reasons for some differences)
- What should be guaranteed is that any XML written using this module must be readable using module as well (“read what I wrote”): that is, we do aim for full round-trip support
- From above: there are XML constructs that module will not be able to handle; including some cases JAXB (and other Java XML libraries) supports
- This module also support constructs and use cases JAXB does not handle: specifically, rich type and object id support of Jackson are supported.
详情可参考官网文档:https://github.com/FasterXML/jackson-dataformat-xml.
2、Jackson 配置
注解 作用域 说明 实现时机 @JacksonXmlRootElement 类上 指定 XML 根标签的名字 序列化时 @JacksonXmlProperty 属性或getter/setter方法上 指定属性对应 XML 映射的名称 序列化和反序列化时 @JacksonXmlText 属性或getter/setter方法上 将属性直接作为未被标签包裹的普通文本 序列化和反序列化时 @JacksonXmlCData 属性或getter/setter方法上 将属性值包裹在 CDATA 标签中 序列化时 @JacksonXmlElementWrapper 属性或getter/setter方法上 类中有集合的属性时,是否生成包裹的标签 序列化时
3、具体使用
Original: https://www.cnblogs.com/wuyongyin/p/16553478.html
Author: 且行且码
Title: Java 操作 XML(14)–jackson-dataformat-xml 使用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620294/
转载文章受原作者版权保护。转载请注明原作者出处!