关于JAXB:
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
JAXB重要的类和接口:
JAXBContext类 是应用的入口,用来管理xml/java的绑定信息
Marshaller接口 其实现可将java object序列化成xml
Unmarshaller接口 其实现可将xml反序列化成java object
JAXB的annotation注解:
@XmlType,将Java类或枚举类型映射到XML模式类型,通常使用其propOrder属性指定字段,序列化的顺序,例如:
@XmlType(propOrder = {"studentId","studentName","country","courses" })
注意:1.@XmlElementWrapper标注的属性,不能出现在@XmlType的propOrder列表中
2.所有@XmlElement标注过的属性,必须出现在@XmlType的propOrder列表中
@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态 的(由@XmlTransient标注)字段到XML。其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序,有两个选项:
XmlAccessOrder.ALPHABETICAL
XmlAccessOrder.UNDEFINED
@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML。
@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。
@XmlRootElement,将Java类或枚举类型映射到XML元素。
@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。
@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。
以上是关于JAXB的介绍,接下来看个实例:
//学生类,其中嵌套了课程类CourseDO的list成员变量
@XmlRootElement(name = "Student") @XmlAccessorType(XmlAccessType.FIELD) public class StudentDO { public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; }
public List<CourseDO> getCourses() { return courses; } public void setCourses(List<CourseDO> courses) { this.courses = courses; } @XmlElement(name = "StudentId") Integer studentId; @XmlElement(name = "StudentName") public String studentName; @XmlElement(name = "Country") String country; @XmlElementWrapper(name = "courselist") @XmlElement(name = "course") List<CourseDO> courses; }
// CourseDO类
@XmlAccessorType(XmlAccessType.FIELD) public class CourseDO { public String getCourseId() { return courseId; } public void setCourseId(String courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } @XmlElement(name = "CourseId") String courseId; @XmlElement(name = "CourseName") String courseName; @XmlElement(name = "Scope") String scope; }
//JAXB的封装类
public class JAXBUtil { private static Map<Class<?>,JAXBContext> contextClassMap = new ConcurrentHashMap<Class<?>,JAXBContext>(); public static JAXBContext getContext(Class<?> type) throws JAXBException { JAXBContext context = contextClassMap.get(type); if(context == null){ context = JAXBContext.newInstance(type); contextClassMap.put(type,context); } return context; } public static String toXml(Object obj) throws Exception { StringWriter writer = new StringWriter(); JAXBContext context = getContext(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT,false); marshaller.marshal(obj,writer); return writer.toString(); } public static Object fromXml(String xml,Class<?> type) throws Exception { StringReader reader = new StringReader(xml); JAXBContext context = getContext(type); Unmarshaller unmarshaller = context.createUnmarshaller(); Object obj = type.cast(unmarshaller.unmarshal(reader)); return obj; } }
//序列化的测试方法
@Test public void testToXml() throws Exception { try{ StudentDO student = new StudentDO(); student.setStudentId(0); student.setStudentName("lily"); student.setCountry("USA"); student.setCourses(getCourseList()); String xml = JAXBUtil.toXml(student); System.out.println(xml); }catch (Exception e){ System.out.println(e.getMessage()); } }
输出结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Student> <StudentId>0</StudentId> <StudentName>lily</StudentName> <Country>USA</Country> <courselist> <course> <CourseId>000</CourseId> <CourseName>Physic</CourseName> <Scope>Science</Scope> </course> <course> <CourseId>001</CourseId> <CourseName>Music</CourseName> <Scope>Art</Scope> </course> </courselist> </Student>
//反序列化的测试方法
@Test
public void testFromXml() throws Exception {
String xml = "<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Student><StudentId>0</StudentId><StudentName>lily</StudentName><Country>USA</Country><Course><CourseId>000</CourseId><CourseName>Physic</CourseName><Scope>Science</Scope></Course><Course><CourseId>001</CourseId><CourseName>Music</CourseName><Scope>Art</Scope></Course><Course><CourseId>002</CourseId><CourseName>Math</CourseName><Scope>Science</Scope></Course></Student>";
try{
StudentDO student = (StudentDO)JAXBUtil.fromXml(xml,StudentDO.class);
int ac = 0;
int dd = ac + 1;
}catch (Exception e){
System.out.println(e.getMessage());
}
} (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|