大家好,又见面了,我是你们的朋友全栈君。
在网上看了许多XML跟Obj互相转换的Demo,但是都很复杂,现在推荐一个极度简单好理解的XML和Obj互转的例子:
JacksonXML,只需要简单的几个注解就能完成XML和Obj的相互转换
假设有如下xml报文:
<?xml version="1.0" encoding="utf-8"?>
<msgbody>
<StringList>
<mx>
<paramA>Aone</paramA>
<paramB>Bone</paramB>
</mx>
<mx>
<paramA>Atow</paramA>
<paramB>Atow</paramB>
</mx>
</StringList>
<serverId>serv10000000156</serverId>
<name>张三</name>
<age>女</age>
<address>武汉市</address>
</msgbody>
分析这段报文,首先报文头是 <?xml version=”1.0″ encoding=”utf-8″?>
然后有<msgbody>标签包住报文体,然后有属性serverId,name,age,address 和 一个列表,列表中的每一个单位用<mx>标签分隔开来,最后每个单位中都有paramA 和 paramB 两个属性。
准备工作添加pom依赖:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.1.0</version>
</dependency>
那么第一步我们创建与这个报文对应的class:
package com.symphony.springbootdata.demo.xmlTest;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.util.List;
/**
* 报文对应class
*
* @author <a href="***@163.com">***</a>
* @version 1.0.0
* @since 1.0.0
* <p>
* Created at 2018/9/30
*/
@JacksonXmlRootElement(localName = "msgbody")
public class Person {
@JacksonXmlProperty(localName = "serverId")
private String serverId;
@JacksonXmlProperty(localName = "name")
private String name;
@JacksonXmlProperty(localName = "age")
private String age;
@JacksonXmlProperty(localName = "address")
private String address;
@JacksonXmlElementWrapper(localName = "StringList")
private List<Clazz> mx;
public Person() {
}
public Person(String serverId, String name, String age, String address, List<Clazz> mx) {
this.serverId = serverId;
this.name = name;
this.age = age;
this.address = address;
this.mx = mx;
}
public String getServerId() {
return serverId;
}
public void setServerId(String serverId) {
this.serverId = serverId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Clazz> getMx() {
return mx;
}
public void setMx(List<Clazz> mx) {
this.mx = mx;
}
@Override
public String toString() {
return "Person{" +
"serverId='" + serverId + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
", mx=" + mx +
'}';
}
}
package com.symphony.springbootdata.demo.xmlTest;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
/**
* 报文中列表对应class
*
* @author <a href="***@163.com">***</a>
* @version 1.0.0
* @since 1.0.0
* <p>
* Created at 2018/10/9
*/
@JacksonXmlRootElement(localName = "MX")
public class Clazz {
@JacksonXmlProperty(localName = "paramA")
private String a;
@JacksonXmlProperty(localName = "paramB")
private String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public String toString() {
return "Clazz{" +
"a='" + a + '\'' +
", b='" + b + '\'' +
'}';
}
}
创建好对应的class之后,开始XML与Obj的转换
package com.symphony.springbootdata.demo.xmlTest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Test;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* XML Obj 互相转换
*
* @author <a href="mailto:***@163.com">***</a>
* @version 1.0.0
* @since 1.0.0
* <p>
* Created at 2018/9/30
*/
public class ObjToXmlUtils {
public static void main(String[] args) throws JsonProcessingException {
List<Clazz> mx = new ArrayList<>();
Clazz clazz = new Clazz();
clazz.setA("Aone");
clazz.setB("Bone");
mx.add(clazz);
clazz = new Clazz();
clazz.setA("Atow");
clazz.setB("Atow");
mx.add(clazz);
ObjectMapper mapper = new XmlMapper();
String s = mapper.writeValueAsString(new Person("serv10000000156", "张三", "女", "武汉市", mx));
System.out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + s);
}
@Test
public void xmlToObj() throws JAXBException, IOException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><msgbody><StringList><mx><paramA>Aone</paramA><paramB>Bone</paramB></mx><mx><paramA>Atow</paramA><paramB>Atow</paramB></mx></StringList><serverId>serv10000000156</serverId><name>张三</name><age>女</age><address>武汉市</address></msgbody>\n";
ObjectMapper mapper = new XmlMapper();
Object po = null;
po = mapper.readValue(xml, Person.class);
System.out.println(po.toString());
}
}
综上,我们看到了注解
@JacksonXmlElementWrapper:指定用于包装List和Map属性的XML元素
@JacksonXmlProperty:为属性指定XML名称空间和本地名称; 以及是否将属性写为XML元素或属性
@JacksonXmlRootElement:指定用于包装根元素的XML元素
@JacksonXmlText:指定当前这个值,没有xml标签包裹
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162488.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...