类级别注解,name属性用于指定生成元素的名字,若不指定,默认使用类名小写作为元素名
//@XmlRootElement(name="mystudent")
@XmlRootElement
public class Student {
}
对应xml:
类级别注解,propOrder属性指定映射XML时的节点顺序
@XmlRootElement
@XmlType(propOrder = {"age", "name"})
public class Student {
private String name
private int age;
}
类级别注解,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 {
}
字段、方法、参数级别的注解,该注解可以被注解的(非静态)字段,或者被注解的get/set方法对应的字段映射为子元素
name属性指定映射时的节点名称,指定生成元素的名字,若不指定,默认使用方法名小写作为元素名
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Student {
private String name
@XmlElement(name="myage")
private int age;
}
字段和方法级别的注解,该注解会将字段或get/set方法对应的字段映射成本类对应元素的属性
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Student {
private String name
@XmlAttribute
private int age;
}
对应xml:
类、字段、方法级别的注解 定义某个字段或属性不需要被映射
生成的XML外围被包裹,name属性指定包裹元素的节点名称
为了在一个XML的Element中添加多个值
@XmlList
private List items;
对应xml:
item1 item2 item3
另:如果这个类包含其他对象作为其属性,被嵌套的类不需要@XmlRootElement注解
/**
* 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对象
*/
public static T xmlToBean(String xml, Class 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 xmlToBean(File file, Class 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 hobbies;
public Student() {
}
public Student(String name, Integer age, List 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 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文件:
zz
18
sing
4
dance
7