下面使用的是JDK自带的类,没有引用任何第三方jar包。
Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 Java 内容对象树。
备注:marshal(序列化、排列、整理)
Marshaller 类使客户端应用程序能够将 Java 内容树转换回 XML 数据。
package hb.jaxb; import javax.xml.bind.annotation.XmlRootElement; //1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象(Classroom)则不需要 @XmlRootElement public class Student { private int id; private String name; private int age; private Classroom classroom; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Classroom getClassroom() { return classroom; } public void setClassroom(Classroom classroom) { this.classroom = classroom; } public Student(int id, String name, int age, Classroom classroom) { super(); this.id = id; this.name = name; this.age = age; this.classroom = classroom; } //无参够着函数一定需要,否则JXBContext无法正常解析。 public Student() { super(); } }
package hb.jaxb; //2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。 public class Classroom { private int id; private String name; private int grade; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public Classroom(int id, String name, int grade) { super(); this.id = id; this.name = name; this.grade = grade; } public Classroom() { super(); } }
package hb.jaxb; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.junit.Test; //Eclipse console //中文乱码解决 eclipse-->Run-->Run Configuration-->Common--> Console Encoding-->Others-->UTF-8 //参考博文:http://hbiao68.iteye.com/blog/1958413 public class TestJaxb { @Test public void beanToXML() { Classroom classroom = new Classroom(1, "软件工程", 4); Student student = new Student(101, "张三", 22, classroom); try { JAXBContext context = JAXBContext.newInstance(Student.class); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(student, System.out); } catch (JAXBException e) { e.printStackTrace(); } } @Test public void XMLStringToBean(){ String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>22</age><classroom><grade>4</grade><id>1</id><name>软件工程</name></classroom><id>101</id><name>张三</name></student>"; try { JAXBContext context = JAXBContext.newInstance(Student.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr)); System.out.println(student.getAge()); System.out.println(student.getClassroom().getName()); } catch (JAXBException e) { e.printStackTrace(); } } }
参考博文:
http://hbiao68.iteye.com/blog/1958413
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1759334