依赖
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
</dependency>
注解说明
- @XmlRootElement,用于类级别的注解,对应xml的跟元素。通过name属性定义这个根节点的名称。
- @XmlAccessorType,定义映射这个类中的何种类型都需要映射到xml。(如果不存在@XmlAccessorType,默认使用XmlAccessType.PUBLIC_MEMBER注解)
参数:XmlAccessType.FIELD: java对象中的所有成员变量。
XmlAccessType.PROPERTY:java对象中所有通过getter/setter方式访问的成员变量。
XmlAccessType.PUBLIC_MEMBER:java对象中所有的public访问权限的成员变量和通过getter/setter方式访问的成员变量。
XmlAccessType.NONE: java对象的所有属性都不映射为xml的元素。
- @XmlAttribute,用于把java对象的属性映射为xml的属性,并可通过name属性为生成的xml属性指定别名。
- @XmlElement,指定一个字段或get/set方法映射到xml的节点。通过name属性定义这个根节点的名称。
- @XmlElementWrapper,为数组或集合定义一个父节点。通过name属性定义这个父节点的名称。
XML格式
<country>
<province></province>
</country>
Java类
@XmlRootElement
public class Country
private String province;
public String getProvince(){
return province;
}
public void setProvince(String province){
this.province = province;
}
}
测试类
public static void main(String[ ] args){
Country country = new Country();
country.setProvince("zhejiang");
JAXBContext context = JAXBContext.newInstance(Country.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
StringWriter writer = new StringWriter();
marshaller.marshal(country, writer);
System.out.println(writer.toString());
}
结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<country>
<province>zhejiang</province>
</coutry>
</xml>
格式1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<country>
<province>zhejiang</province>
<province>jiangsu</province>
</coutry>
</xml>
修改1
@XmlRootElement
public class Country
{
private List<String> province;
public String getProvince(){
return province;
}
public void setProvince(List<String> province){
this.province = province;
}
public static class Province{
}
}
测试1
public static void main(String[ ] args){
Country country = new Country();
List<String> province = new ArrayList<String>();
province.add("zhejiang");
province.add("jiangsu");
JAXBContext context = JAXBContext.newInstance(Country.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
StringWriter writer = new StringWriter();
marshaller.marshal(country, writer);
System.out.println(writer.toString());
}
格式2
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<country>
<provinceList>
<province>zhejiang</province>
<province>jiangsu</province>
</proviceList>
</coutry>
</xml>
修改2
@XmlRootElement
public class Country
{
@XmlElementWrapper
private List<String> province;
public String getProvince(){
return province;
}
public void setProvince(List<String> province){
this.province = province;
}
public static class Province{
}
}
格式3
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<country>
<provinceList>
<province-name>zhejiang</province-name>
<province-name>jiangsu</province-name>
</proviceList>
</coutry>
</xml>
修改3
@XmlRootElement
public class Country
{
@XmlElementWrapper
private List<String> province;
@XmlElement(name="province-name")
public String getProvince(){
return province;
}
public void setProvince(List<String> province){
this.province = province;
}
public static class Province{
}
}
++在getProvince上面加了一个@XmlElement注解,搞定!++
@XmlElementWrapper和@XmlElement这两个注解的概念
@XmlElementWrapper注解表示生成一个包装器元素,仅允许出现在集合属性上。
@XmlElement注解指定一个字段或get/set方法映射到XML的节点,并且可以通过name属性改变java对象属性在XML节点中的名字。
package com.noah.project.util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
/**
* @date: 2018/04/13
* @author: wangcd
* @version: 1.0.0
* @description:Jaxb2工具类.
**/
public class JaxbUtil {
/**
* 以UTF-8编码的格式将JavaBean转成XML
* @param o
* @return
*/
public static String convertToXml(Object o){
return convertToXml(o,"UTF-8");
}
/**
* 将JavaBean转成XML
* @param obj JavaBean对象
* @param encoding xml编码格式,例,"UTF-8"
* @return
*/
public static String convertToXml(Object obj,String encoding){
String result = "";
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 将XML转成JavaBean
* @param xml xml字符串
* @param cls JavaBean的class名称 例,Object.class
* @param <T>
* @return 返回转换后的JavaBean
*/
public static <T> T convertToJavaBean(String xml,Class<T> cls){
T t = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(cls);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (JAXBException e) {
e.printStackTrace();
}
return t;
}
}
网友评论