使⽤jackson把java对象转成xml格式
⼀、maven依赖引⼊
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.0</version>
</dependency>
java xml是什么<dependency>
<groupId>com.</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
⼆、xml注解介绍
jackson提供了xml格式的转化,主要包含以下注解:
1、@JacksonXmlRootElement
namespace属性:⽤于指定XML根元素命名空间的名称。
localname属性:⽤于指定XML根元素节点标签的名称。
2、@JacksonXmlProperty
namespace和localname属性⽤于指定XML命名空间的名称,isAttribute指定该属 性作为XML的属性()还是作为⼦标签().
3、@JacksonXmlText注解将属性直接作为未被标签包裹的普通⽂本表现。
4、@JacksonXmlCData将属性包裹在CDATA标签中。
集合元素的映射
5、@JacksonXmlElementWrapper可以将列表数据转为XML节点。
useWrapping属性设置是否设置外围标签名,默认true
public class XmlUtil {
private static XmlMapper xmlMapper =new XmlMapper();
private static XmlMapper getXmlMapper(){
xmlMapper.setDefaultUseWrapper(false);
//字段为null,⾃动忽略,不再序列化
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//XML标签名:使⽤骆驼命名的属性名,
xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
//设置转换模式
return xmlMapper;
}
public static String toXml(Object value){
try{
String xml =getXmlMapper().writeValueAsString(value);
return xml;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
例1:设置根标签@JacksonXmlRootElement
@JacksonXmlRootElement(localName ="root")
public class Person {
@JacksonXmlProperty
private String documentNo;
@JacksonXmlProperty
private String orderNo;
private List<Student> students;
可以看到根标签已经是root了
<root>
<DocumentNo>342342423</DocumentNo>
<OrderNo>00999922222</OrderNo>
<Student>
<Name>李四</Name>
<Age>12</Age>
</Student>
<Student>
<Name>王五</Name>
</Student>
</root>
例2:@JacksonXmlProperty 如果不设置localName默认都是⾸字母⼤写
@JacksonXmlRootElement(localName ="root")
public class Person {
@JacksonXmlProperty(localName ="documentNo",isAttribute =true)
private String documentNo;
@JacksonXmlProperty(localName ="orderNo")
private String orderNo;
@JacksonXmlProperty(localName ="student")
private List<Student> students;
执⾏结果全部标签名全部变成⼩写
<root documentNo="342342423">
<student>
<name>李四</name>
<age>12</age>
</student>
<student>
<name>王五</name>
</student>
<orderNo>00999922222</orderNo>
</root>
例3:@JacksonXmlElementWrapper设置集合外围标签名
@JacksonXmlRootElement(localName ="root")
public class Person {
@JacksonXmlProperty(localName ="documentNo")
private String documentNo;
@JacksonXmlProperty(localName ="orderNo")
private String orderNo;
@JacksonXmlElementWrapper(localName ="students")
@JacksonXmlProperty(localName ="student")
private List<Student> students;
执⾏结果
<root>
<students>
<student>
<name>李四</name>
<age>12</age>
</student>
<student>
<name>王五</name>
</student>
</students>
<documentNo>342342423</documentNo>
<orderNo>00999922222</orderNo>
</root>
例4: @JacksonXmlText注解将属性直接作为未被标签包裹的普通⽂本表现
@JacksonXmlRootElement(localName ="root")
public class Person {
@JacksonXmlText
private String documentNo;
@JacksonXmlProperty(localName ="orderNo")
private String orderNo;
@JacksonXmlElementWrapper(localName ="students")
@JacksonXmlProperty(localName ="student")
private List<Student> students;
执⾏结果
<root>342342423
<students>
<student>
<name>李四</name>
<age>12</age>
</student>
<student>
<name>王五</name>
</student>
</students>
<orderNo>00999922222</orderNo>
</root>
例5:@JacksonXmlCData将属性包裹在CDATA标签中。
@JacksonXmlRootElement(localName ="root")
public class Person {
@JacksonXmlCData
private String documentNo;
@JacksonXmlProperty(localName ="orderNo")
private String orderNo;
@JacksonXmlElementWrapper(localName ="students")
@JacksonXmlProperty(localName ="student")
private List<Student> students;
执⾏结果
<root>
<DocumentNo>
<![CDATA[342342423]]>
</DocumentNo>
<students>
<student>
<name>李四</name>
<age>12</age>
</student>
<student>
<name>王五</name>
</student>
</students>
<orderNo>00999922222</orderNo>
</root>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。