一、JavaBean注解
- 1. @XmlRootElement
类级别注解,name属性用于指定生成元素的名字,若不指定,默认使用类名小写作为元素名
//@XmlRootElement(name="mystudent")
@XmlRootElement
public class Student {
}
对应xml:
<student>
</student>
- 2. @XmlType
类级别注解,propOrder属性指定映射XML时的节点顺序
@XmlRootElement
@XmlType(propOrder = {"age", "name"})
public class Student {
private String name
private int age;
}
- 3. @XmlAccessorType
类级别注解,value属性指定这个类中何种类型需要映射到XML
XmlAccessType.FIELD:映射这个类中的所有字段到XML
XmlAccessType.PROPERTY:映射这个类中的属性(get/set方法)到XML
XmlAccessType.PUBLIC_MEMBER:将这个类中的所有public的field或property同时映射到XML(默认)
XmlAccessType.NONE:不映射
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Student {
}
- 4. @XmlElement
字段、方法、参数级别的注解,该注解可以被注解的(非静态)字段,或者被注解的get/set方法对应的字段映射为子元素
name属性指定映射时的节点名称,指定生成元素的名字,若不指定,默认使用方法名小写作为元素名
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Student {
private String name
@XmlElement(name="myage")
private int age;
}
- 5. @XmlAttribute
字段和方法级别的注解,该注解会将字段或get/set方法对应的字段映射成本类对应元素的属性
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Student {
private String name
@XmlAttribute
private int age;
}
对应xml:
<student age="10">
- 6. @XmlTransient
类、字段、方法级别的注解 定义某个字段或属性不需要被映射
- 7. @XmlElementWrapper
生成的XML外围被包裹,name属性指定包裹元素的节点名称
- 8. @XmlList
为了在一个XML的Element中添加多个值
@XmlList
private List<String> items;
对应xml:
<items>item1 item2 item3</items>
另:如果这个类包含其他对象作为其属性,被嵌套的类不需要@XmlRootElement注解
二、JavaBean转换为XML
/**
* JavaBean对象转换xml字符串
*/
public static String objectToXML(Object object) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller m = context.createMarshaller();
// 设置格式化输出
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Writer w = new StringWriter();
m.marshal(object, w);
return w.toString();
}
/**
* JavaBean对象转换xml文件
*/
public static File objectToXML(Object object, String fileName) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller m = context.createMarshaller();
// 设置格式化输出
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
File file = new File(fileName);
m.marshal(object, file);
return file;
}
三、XML转换为JavaBean
/**
* xml字符串转换为JavaBean对象
*/
public static <T> T xmlToBean(String xml, Class<T> clazz) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller um = context.createUnmarshaller();
StringReader sr = new StringReader(xml);
return (T)um.unmarshal(sr);
}
/**
* xml文件转换为JavaBean对象
*/
public static <T> T xmlToBean(File file, Class<T> clazz) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller um = context.createUnmarshaller();
return (T)um.unmarshal(file);
}
四、测试
XmlAccessType.PUBLIC_MEMBER:将这个类中的所有public的field或property同时映射到XML(默认)
所有为了测试方便,就不写set/get方法,把字段设置为public即可
Student.java
@XmlRootElement
public class Student {
public String name;
public Integer age;
@XmlElementWrapper(name = "hobbylist")
private List<Hobby> hobbies;
public Student() {
}
public Student(String name, Integer age, List<Hobby> hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
}
Hobby.java
public class Hobby {
public String name;
public int grade;
public Hobby() {
}
public Hobby(String name, int grade) {
this.name = name;
this.grade = grade;
}
}
Test.java
把上面四个转换方法,封装到XMLHelper类中,直接用XMLHelper类调用其静态方法
public class Test {
public static void main(String[] args) throws JAXBException {
List<Hobby> hobbies = new ArrayList<>();
Hobby hobby1 = new Hobby("sing",4);
Hobby hobby2 = new Hobby("dance",7);
hobbies.add(hobby1);
hobbies.add(hobby2);
Student student = new Student("zz",18,hobbies);
XMLHelper.objectToXML(student, "p.xml");
}
}
生成p.xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<name>zz</name>
<age>18</age>
<hobbylist>
<hobbies>
<name>sing</name>
<grade>4</grade>
</hobbies>
<hobbies>
<name>dance</name>
<grade>7</grade>
</hobbies>
</hobbylist>
</student>