jaxb的使用2 ,包含格式化xml文件

前端之家收集整理的这篇文章主要介绍了jaxb的使用2 ,包含格式化xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
http://blog.csdn.net/wugouzi/article/details/25044655/ 一、简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到 XML实例文档。 Jaxb 2.0是JDK 1.6的组成部分。我们不需要下载第三方jar包 即可做到轻松转换。Jaxb2使用了JDK的新特性,如:Annotation、GenericType等,需要在即将转换的JavaBean中添加annotation注解。 二、重要概念 •JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。 •Marshaller接口,将Java对象序列化为XML数据。 •Unmarshaller接口,将XML数据反序列化为Java对象。 •@XmlType,将Java类或枚举类型映射到XML模式类型 •@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标 注)字段到XML。其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。 •@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。 •@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML。 •@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。 •@XmlRootElement,将Java类或枚举类型映射到XML元素。 •@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。 •@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。 三、示例 1、准备工作 Java代码 收藏代码 1. package utils; 2. 3. import java.io.StringReader; 4. import java.io.StringWriter; 5. 6. import javax.xml.bind.JAXBContext; 7. import javax.xml.bind.Marshaller; 8. import javax.xml.bind.Unmarshaller; 9. 10. /** 11. * Jaxb2工具类 12. * @author zhuc 13. * @create 2013-3-29 下午2:40:14 14. */ 15. public class JaxbUtil { 16. 17. /** 18. * JavaBean转换成xml 19. * 默认编码UTF-8 20. * @param obj 21. * @param writer 22. * @return 23. */ 24. public static String convertToXml(Object obj) { 25. return convertToXml(obj,"UTF-8"); 26. } 27. 28. /** 29. * JavaBean转换成xml 30. * @param obj 31. * @param encoding 32. * @return 33. */ 34. public static String convertToXml(Object obj,String encoding) { 35. String result = null; 36. try { 37. JAXBContext context = JAXBContext.newInstance(obj.getClass()); 38. Marshaller marshaller = context.createMarshaller(); 39. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); 40. marshaller.setProperty(Marshaller.JAXB_ENCODING,encoding); 41. 42. StringWriter writer = new StringWriter(); 43. marshaller.marshal(obj,writer); 44. result = writer.toString(); 45. } catch (Exception e) { 46. e.printStackTrace(); 47. } 48. 49. return result; 50. } 51. 52. /** 53. * xml转换成JavaBean 54. * @param xml 55. * @param c 56. * @return 57. */ 58. @SuppressWarnings("unchecked") 59. public static <T> T converyToJavaBean(String xml,Class<T> c) { 60. T t = null; 61. try { 62. JAXBContext context = JAXBContext.newInstance(c); 63. Unmarshaller unmarshaller = context.createUnmarshaller(); 64. t = (T) unmarshaller.unmarshal(new StringReader(xml)); 65. } catch (Exception e) { 66. e.printStackTrace(); 67. } 68. 69. return t; 70. } 71. } 非常简单易懂,需要注意的是 Java代码 收藏代码 1. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); 2. marshaller.setProperty(Marshaller.JAXB_ENCODING,encoding); Marshaller.JAXB_FORMATTED_OUTPUT 决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml) Marshaller.JAXB_ENCODING xml的编码方式 另外,Marshaller 还有其他Property可以设置,可以去查阅api。 2、最简单转换 Java代码 收藏代码 1. package t1; 2. 3. import java.util.Date; 4. 5. import javax.xml.bind.annotation.XmlAccessType; 6. import javax.xml.bind.annotation.XmlAccessorType; 7. import javax.xml.bind.annotation.XmlAttribute; 8. import javax.xml.bind.annotation.XmlElement; 9. import javax.xml.bind.annotation.XmlRootElement; 10. import javax.xml.bind.annotation.XmlType; 11. 12. /** 13. * @author zhuc 14. * @create 2013-3-29 下午2:49:48 15. */ 16. @XmlAccessorType(XmlAccessType.FIELD) 17. @XmlRootElement 18. @XmlType(name = "book",propOrder = { "author","calendar","price","id" }) 19. public class Book { 20. 21. @XmlElement(required = true) 22. private String author; 23. 24. @XmlElement(name = "price_1",required = true) 25. private float price; 26. 27. @XmlElement 28. private Date calendar; 29. 30. @XmlAttribute 31. private Integer id; 32. 33. /** 34. * @return the author 35. */ 36. public String getAuthor() { 37. return author; 38. } 39. 40. /** 41. * @return the price 42. */ 43. public float getPrice() { 44. return price; 45. } 46. 47. /** 48. * @return the calendar 49. */ 50. public Date getCalendar() { 51. return calendar; 52. } 53. 54. /** 55. * @return the id 56. */ 57. public Integer getId() { 58. return id; 59. } 60. 61. /** 62. * @param author the author to set 63. */ 64. public void setAuthor(String author) { 65. this.author = author; 66. } 67. 68. /** 69. * @param price the price to set 70. */ 71. public void setPrice(float price) { 72. this.price = price; 73. } 74. 75. /** 76. * @param calendar the calendar to set 77. */ 78. public void setCalendar(Date calendar) { 79. this.calendar = calendar; 80. } 81. 82. /** 83. * @param id the id to set 84. */ 85. public void setId(Integer id) { 86. this.id = id; 87. } 88. 89. /* (non-Javadoc) 90. * @see java.lang.Object#toString() 91. */ 92. @Override 93. public String toString() { 94. return "Book [author=" + author + ",price=" + price + ",calendar=" + calendar + ",id=" + id + "]"; 95. } 96. 97. } Java代码 收藏代码 1. package t1; 2. 3. import java.util.Date; 4. 5. import javax.xml.bind.JAXBException; 6. 7. import org.junit.Test; 8. 9. import utils.JaxbUtil; 10. 11. /** 12. * @author zhuc 13. * @create 2013-3-29 下午2:50:00 14. */ 15. public class JaxbTest1 { 16. 17. /** 18. * @throws JAXBException 19. */ 20. @Test 21. public void showMarshaller() { 22. Book book = new Book(); 23. book.setId(100); 24. book.setAuthor("James"); 25. book.setCalendar(new Date()); 26. book.setPrice(23.45f); //默认是0.0 27. 28. String str = JaxbUtil.convertToXml(book); 29. System.out.println(str); 30. } 31. 32. /** 33. * @throws JAXBException 34. */ 35. @Test 36. public void showUnMarshaller() { 37. String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + 38. "<book id=\"100\">" + 39. " <author>James</author>" + 40. " <calendar>2013-03-29T09:25:56.004+08:00</calendar>" + 41. " <price_1>23.45</price_1>" + 42. "</book>"; 43. 44. Book book = JaxbUtil.converyToJavaBean(str,Book.class); 45. System.out.println(book); 46. } 47. } 输出结果分别为: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <book id="100"> <author>James</author> <calendar>2013-03-29T14:50:58.974+08:00</calendar> <price_1>23.45</price_1> </book> Book [author=James,price=23.45,calendar=Fri Mar 29 09:25:56 CST 2013,id=100] 3、类中包含复杂对象的转换 Java代码 收藏代码 1. package t2; 2. 3. import javax.xml.bind.annotation.XmlAccessType; 4. import javax.xml.bind.annotation.XmlAccessorType; 5. import javax.xml.bind.annotation.XmlAttribute; 6. import javax.xml.bind.annotation.XmlElement; 7. import javax.xml.bind.annotation.XmlRootElement; 8. import javax.xml.bind.annotation.XmlType; 9. 10. /** 11. * @author zhuc 12. * @create 2013-3-29 下午2:51:44 13. */ 14. @XmlAccessorType(XmlAccessType.FIELD) 15. @XmlRootElement(name = "student") 16. @XmlType(propOrder = {}) 17. public class Student { 18. 19. @XmlAttribute 20. private Integer id; 21. 22. @XmlElement 23. private String name; 24. 25. @XmlElement(name = "role") 26. private Role role; 27. 28. /** 29. * @return the id 30. */ 31. public Integer getId() { 32. return id; 33. } 34. 35. /** 36. * @return the name 37. */ 38. public String getName() { 39. return name; 40. } 41. 42. /** 43. * @return the role 44. */ 45. public Role getRole() { 46. return role; 47. } 48. 49. /** 50. * @param id the id to set 51. */ 52. public void setId(Integer id) { 53. this.id = id; 54. } 55. 56. /** 57. * @param name the name to set 58. */ 59. public void setName(String name) { 60. this.name = name; 61. } 62. 63. /** 64. * @param role the role to set 65. */ 66. public void setRole(Role role) { 67. this.role = role; 68. } 69. 70. /* (non-Javadoc) 71. * @see java.lang.Object#toString() 72. */ 73. @Override 74. public String toString() { 75. return "Student [id=" + id + ",name=" + name + ",role=" + role + "]"; 76. } 77. 78. } Java代码 收藏代码 1. package t2; 2. 3. import javax.xml.bind.annotation.XmlAccessType; 4. import javax.xml.bind.annotation.XmlAccessorType; 5. import javax.xml.bind.annotation.XmlElement; 6. import javax.xml.bind.annotation.XmlType; 7. 8. /** 9. * @author zhuc 10. * @create 2013-3-29 下午2:51:52 11. */ 12. @XmlAccessorType(XmlAccessType.FIELD) 13. @XmlType(propOrder = { "name","desc" }) 14. public class Role { 15. 16. @XmlElement 17. private String name; 18. 19. @XmlElement 20. private String desc; 21. 22. /** 23. * @return the name 24. */ 25. public String getName() { 26. return name; 27. } 28. 29. /** 30. * @return the desc 31. */ 32. public String getDesc() { 33. return desc; 34. } 35. 36. /** 37. * @param name the name to set 38. */ 39. public void setName(String name) { 40. this.name = name; 41. } 42. 43. /** 44. * @param desc the desc to set 45. */ 46. public void setDesc(String desc) { 47. this.desc = desc; 48. } 49. 50. /* (non-Javadoc) 51. * @see java.lang.Object#toString() 52. */ 53. @Override 54. public String toString() { 55. return "Role [name=" + name + ",desc=" + desc + "]"; 56. } 57. 58. } Java代码 收藏代码 1. package t2; 2. 3. import org.junit.Test; 4. 5. import utils.JaxbUtil; 6. 7. /** 8. * @author zhuc 9. * @create 2013-3-29 下午2:52:00 10. */ 11. public class JaxbTest2 { 12. 13. @Test 14. public void showMarshaller() { 15. 16. Student student = new Student(); 17. student.setId(12); 18. student.setName("test"); 19. 20. Role role = new Role(); 21. role.setDesc("管理"); 22. role.setName("班长"); 23. 24. student.setRole(role); 25. 26. String str = JaxbUtil.convertToXml(student); 27. System.out.println(str); 28. } 29. 30. @Test 31. public void showUnMarshaller() { 32. String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+ 33. "<student id=\"12\">"+ 34. " <name>test</name>"+ 35. " <role>"+ 36. " <name>班长</name>"+ 37. " <desc>管理</desc>"+ 38. "</role>"+ 39. "</student>"; 40. Student student = JaxbUtil.converyToJavaBean(str,Student.class); 41. System.out.println(student); 42. } 43. 44. } 输出结果分别为: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <student id="12"> <name>test</name> <role> <name>班长</name> <desc>管理</desc> </role> </student> Student [id=12,name=test,role=Role [name=班长,desc=管理]] 4、集合对象的转换(同样适用于Set) Java代码 收藏代码 1. package t3; 2. 3. import java.util.List; 4. 5. import javax.xml.bind.annotation.XmlAccessType; 6. import javax.xml.bind.annotation.XmlAccessorType; 7. import javax.xml.bind.annotation.XmlElement; 8. import javax.xml.bind.annotation.XmlElementWrapper; 9. import javax.xml.bind.annotation.XmlRootElement; 10. import javax.xml.bind.annotation.XmlType; 11. 12. /** 13. * @author zhuc 14. * @create 2013-3-29 下午2:55:56 15. */ 16. @XmlAccessorType(XmlAccessType.FIELD) 17. @XmlRootElement(name = "country") 18. @XmlType(propOrder = { "name","provinceList" }) 19. public class Country { 20. 21. @XmlElement(name = "country_name") 22. private String name; 23. 24. @XmlElementWrapper(name = "provinces") 25. @XmlElement(name = "province") 26. private List<Province> provinceList; 27. 28. /** 29. * @return the name 30. */ 31. public String getName() { 32. return name; 33. } 34. 35. /** 36. * @return the provinceList 37. */ 38. public List<Province> getProvinceList() { 39. return provinceList; 40. } 41. 42. /** 43. * @param name the name to set 44. */ 45. public void setName(String name) { 46. this.name = name; 47. } 48. 49. /** 50. * @param provinceList the provinceList to set 51. */ 52. public void setProvinceList(List<Province> provinceList) { 53. this.provinceList = provinceList; 54. } 55. 56. /* (non-Javadoc) 57. * @see java.lang.Object#toString() 58. */ 59. @Override 60. public String toString() { 61. return "Country [name=" + name + ",provinceList=" + provinceList + "]"; 62. } 63. 64. } Java代码 收藏代码 1. package t3; 2. 3. import javax.xml.bind.annotation.XmlAccessType; 4. import javax.xml.bind.annotation.XmlAccessorType; 5. import javax.xml.bind.annotation.XmlElement; 6. import javax.xml.bind.annotation.XmlType; 7. 8. /** 9. * @author zhuc 10. * @create 2013-3-29 下午2:56:03 11. */ 12. @XmlAccessorType(XmlAccessType.FIELD) 13. @XmlType(propOrder = { "name","provCity" }) 14. public class Province { 15. 16. @XmlElement(name = "province_name") 17. private String name; 18. 19. @XmlElement(name = "prov_city") 20. private String provCity; 21. 22. /** 23. * @return the provCity 24. */ 25. public String getProvCity() { 26. return provCity; 27. } 28. 29. /** 30. * @param provCity the provCity to set 31. */ 32. public void setProvCity(String provCity) { 33. this.provCity = provCity; 34. } 35. 36. /** 37. * @return the name 38. */ 39. public String getName() { 40. return name; 41. } 42. 43. /** 44. * @param name the name to set 45. */ 46. public void setName(String name) { 47. this.name = name; 48. } 49. 50. /* (non-Javadoc) 51. * @see java.lang.Object#toString() 52. */ 53. @Override 54. public String toString() { 55. return "Province [name=" + name + ",provCity=" + provCity + "]"; 56. } 57. 58. } Java代码 收藏代码 1. package t3; 2. 3. import java.util.ArrayList; 4. import java.util.List; 5. 6. import org.junit.Test; 7. 8. import utils.JaxbUtil; 9. 10. /** 11. * @author zhuc 12. * @create 2013-3-29 下午2:56:11 13. */ 14. public class JaxbTest3 { 15. 16. /** 17. * @throws JAXBException 18. */ 19. @Test 20. public void showMarshaller() { 21. Country country = new Country(); 22. country.setName("中国"); 23. 24. List<Province> list = new ArrayList<Province>(); 25. Province province = new Province(); 26. province.setName("江苏省"); 27. province.setProvCity("南京市"); 28. Province province2 = new Province(); 29. province2.setName("浙江省"); 30. province2.setProvCity("杭州市"); 31. list.add(province); 32. list.add(province2); 33. 34. country.setProvinceList(list); 35. 36. String str = JaxbUtil.convertToXml(country); 37. System.out.println(str); 38. } 39. 40. /** 41. * 42. */ 43. @Test 44. public void showUnMarshaller() { 45. String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+ 46. "<country>"+ 47. " <country_name>中国</country_name>"+ 48. " <provinces>"+ 49. " <province>"+ 50. " <province_name>江苏省</province_name>"+ 51. " <prov_city>南京市</prov_city>"+ 52. " </province>"+ 53. " <province>"+ 54. " <province_name>浙江省</province_name>"+ 55. " <prov_city>杭州市</prov_city>"+ 56. " </province>"+ 57. " </provinces>"+ 58. "</country>"; 59. Country country = JaxbUtil.converyToJavaBean(str,Country.class); 60. System.out.println(country); 61. } 62. 63. } 输出结果分别为: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <country> <country_name>中国</country_name> <provinces> <province> <province_name>江苏省</province_name> <prov_city>南京市</prov_city> </province> <province> <province_name>浙江省</province_name> <prov_city>杭州市</prov_city> </province> </provinces> </country> Country [name=中国,provinceList=[Province [name=江苏省,provCity=南京市],Province [name=浙江省,provCity=杭州市]]]

猜你在找的XML相关文章