JAVA XML转对象 对象转XML

在网上看了许多XML跟Obj互相转换的Demo,但是都很复杂,现在推荐一个极度简单好理解的XML和Obj互转的例子:

JacksonXML,只需要简单的几个注解就能完成XML和Obj的相互转换

假设有如下xml报文:



  
    
      Aone
      Bone
    
    
      Atow
      Atow
    
  
  serv10000000156
  张三
  
  
武汉市

分析这段报文,首先报文头是 

然后有标签包住报文体,然后有属性serverId,name,age,address 和 一个列表,列表中的每一个单位用标签分隔开来,最后每个单位中都有paramA 和 paramB 两个属性。

准备工作添加pom依赖:


	com.fasterxml.jackson.dataformat
	jackson-dataformat-xml
	2.9.0


	com.fasterxml.woodstox
	woodstox-core
	5.1.0

那么第一步我们创建与这个报文对应的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 ***
 * @version 1.0.0
 * @since 1.0.0
 * 

* 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 mx; public Person() { } public Person(String serverId, String name, String age, String address, List 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 getMx() { return mx; } public void setMx(List 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 ***
 * @version 1.0.0
 * @since 1.0.0
 * 

* 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 ***
 * @version 1.0.0
 * @since 1.0.0
 * 

* Created at 2018/9/30 */ public class ObjToXmlUtils { public static void main(String[] args) throws JsonProcessingException { List 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("" + s); } @Test public void xmlToObj() throws JAXBException, IOException { String xml = "AoneBoneAtowAtowserv10000000156张三

武汉市
\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标签包裹

你可能感兴趣的:(java其他)