使用JAXB将xml转成Java对象
工作中遇到个问题,需要将xml转对象,之前复杂的xml都是自己用dom4j来解析组装成Java对象,但是对于简单的,看到了JAXB处理的这种方式,就整理下,写成个工具类。
1. xml内容
city.xml
<?xml version="1.0" encoding="UTF-8" ?>
<c c1="0">
<a>10</a>
<b>20</b>
<d d1="101280101" d2="重庆" d3="nanping" d4="南坪"/>
<d d1="101280102" d2="重庆" d3="yubei" d4="渝北"/>
<d d1="101280103" d2="重庆" d3="dadukou" d4="大渡口"/>
</c>
2. Java对象类
- CityList.xml
package com.stormkai.jaxb;
import lombok.Data;
import javax.xml.bind.annotation.*;
import java.util.List;
@Data
@XmlRootElement(name = "c")
@XmlAccessorType(XmlAccessType.FIELD)
public class CityList {
@XmlAttribute(name = "c1")
private String c1;
@XmlElement(name = "a")
private String a;
@XmlElement(name = "b")
private String b;
@XmlElement(name = "d")
private List<City> cityList;
}
- City.java
package com.stormkai.jaxb;
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@Data
@XmlRootElement(name = "d")
@XmlAccessorType(XmlAccessType.FIELD)
public class City {
@XmlAttribute(name = "d1")
private String cityId;
@XmlAttribute(name = "d2")
private String cityName;
@XmlAttribute(name = "d3")
private String cityNode;
@XmlAttribute(name = "d4")
private String area;
}
JAXBContext类,是应用的入口,用于管理XML/Java绑定信息
Unmarshaller接口,将XML数据反序列化为Java对象
@XmlType,将Java类或枚举类型映射到XML模式类型
@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化
- PUBLIC_MEMBER :默认值,public权限亦或是具有get/set方法属性
- FIELD:对象中的所有属性
- PROPERT:对象中所有具备get/set方法的属性
- NONE:对象的所有属性都不映射
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序
@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML
@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)
@XmlRootElement,将Java类或枚举类型映射到XML元素
@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素
@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性
3. 封装的工具类
XmlBuilder.java
package com.stormkai.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.StringReader;
public class XmlBuilder {
public static <T> T xmlToObj4Jaxb(Class<?> clazz, String xml){
try {
T xmlObject;
Reader reader;
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xml);
xmlObject = (T)unmarshaller.unmarshal(reader);
reader.close();
return xmlObject;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4. 测试
public class XmlBuilder {
public static <T> T xmlToObj4Jaxb(Class<?> clazz, String xml){
try {
T xmlObject;
Reader reader;
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xml);
xmlObject = (T)unmarshaller.unmarshal(reader);
reader.close();
return xmlObject;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getCityXml1(){
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"<c c1=\"0\">\n" +
" <a>10</a>\n" +
" <b>20</b>\n" +
" <d d1=\"101280101\" d2=\"重庆\" d3=\"nanping\" d4=\"南坪\"/>\n" +
" <d d1=\"101280102\" d2=\"重庆\" d3=\"yubei\" d4=\"渝北\"/>\n" +
" <d d1=\"101280103\" d2=\"重庆\" d3=\"dadukou\" d4=\"大渡口\"/>\n" +
"</c>";
return xml;
}
public static void main(String[] args) {
String xml = new XmlBuilder().getCityXml1();
CityList cityList = xmlToObj4Jaxb(CityList.class, xml);
System.out.println(JSON.toJSONString(cityList));
}
}
输出结果:文章来源:https://www.toymoban.com/news/detail-731293.html
{"a":"10","b":"20","c1":"0","cityList":[{"area":"南坪","cityId":"101280101","cityName":"重庆","cityNode":"nanping"},{"area":"渝北","cityId":"101280102","cityName":"重庆","cityNode":"yubei"},{"area":"大渡口","cityId":"101280103","cityName":"重庆","cityNode":"dadukou"}]}
Process finished with exit code 0
输出的json格式化后:文章来源地址https://www.toymoban.com/news/detail-731293.html
{
"a": "10",
"b": "20",
"c1": "0",
"cityList": [{
"area": "南坪",
"cityId": "101280101",
"cityName": "重庆",
"cityNode": "nanping"
}, {
"area": "渝北",
"cityId": "101280102",
"cityName": "重庆",
"cityNode": "yubei"
}, {
"area": "大渡口",
"cityId": "101280103",
"cityName": "重庆",
"cityNode": "dadukou"
}]
}
到了这里,关于使用JAXB将xml转成Java对象的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!