写点什么

Jaxb2 实现 JavaBean 与 xml 互转

发布于: 2021 年 11 月 07 日

Java 代码??



  1. package?t1;??

  2. import?java.util.Date;??

  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.annotat


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ion.XmlRootElement;??


  1. import?javax.xml.bind.annotation.XmlType;??

  2. /**?

  3. ?*?@author??????zhuc?

  4. ?*?@create??????2013-3-29?下午 2:49:48?

  5. ?*/??

  6. @XmlAccessorType(XmlAccessType.FIELD)??

  7. @XmlRootElement??

  8. @XmlType(name?=?"book",?propOrder?=?{?"author",?"calendar",?"price",?"id"?})??

  9. public?class?Book?{??

  10. ????@XmlElement(required?=?true)??

  11. ????private?String?author;??

  12. ????@XmlElement(name?=?"price_1",?required?=?true)??

  13. ????private?float?price;??

  14. ????@XmlElement??

  15. ????private?Date?calendar;??

  16. ????@XmlAttribute??

  17. ????private?Integer?id;??

  18. ????/**?

  19. ?????*?@return?the?author?

  20. ?????*/??

  21. ????public?String?getAuthor()?{??

  22. ????????return?author;??

  23. ????}??

  24. ????/**?

  25. ?????*?@return?the?price?

  26. ?????*/??

  27. ????public?float?getPrice()?{??

  28. ????????return?price;??

  29. ????}??

  30. ????/**?

  31. ?????*?@return?the?calendar?

  32. ?????*/??

  33. ????public?Date?getCalendar()?{??

  34. ????????return?calendar;??

  35. ????}??

  36. ????/**?

  37. ?????*?@return?the?id?

  38. ?????*/??

  39. ????public?Integer?getId()?{??

  40. ????????return?id;??

  41. ????}??

  42. ????/**?

  43. ?????*?@param?author?the?author?to?set?

  44. ?????*/??

  45. ????public?void?setAuthor(String?author)?{??

  46. ????????this.author?=?author;??

  47. ????}??

  48. ????/**?

  49. ?????*?@param?price?the?price?to?set?

  50. ?????*/??

  51. ????public?void?setPrice(float?price)?{??

  52. ????????this.price?=?price;??

  53. ????}??

  54. ????/**?

  55. ?????*?@param?calendar?the?calendar?to?set?

  56. ?????*/??

  57. ????public?void?setCalendar(Date?calendar)?{??

  58. ????????this.calendar?=?calendar;??

  59. ????}??

  60. ????/**?

  61. ?????*?@param?id?the?id?to?set?

  62. ?????*/??

  63. ????public?void?setId(Integer?id)?{??

  64. ????????this.id?=?id;??

  65. ????}??

  66. ????/*?(non-Javadoc)?

  67. ?????*?@see?java.lang.Object#toString()?

  68. ?????*/??

  69. ????@Override??

  70. ????public?String?toString()?{??

  71. ????????return?"Book?[author="?+?author?+?",?price="?+?price?+?",?calendar="?+?calendar?+?",?id="?+?id?+?"]";??

  72. ????}??

  73. }??


Java 代码??



  1. package?t1;??

  2. import?java.util.Date;??

  3. import?javax.xml.bind.JAXBException;??

  4. import?org.junit.Test;??

  5. import?utils.JaxbUtil;??

  6. /**?

  7. ?*?@author??????zhuc?

  8. ?*?@create??????2013-3-29?下午 2:50:00?

  9. ?*/??

  10. public?class?JaxbTest1?{??

  11. ????/**?

  12. ?????*?@throws?JAXBException?

  13. ?????*/??

  14. ????@Test??

  15. ????public?void?showMarshaller()??{??

  16. ????????Book?book?=?new?Book();??

  17. ????????book.setId(100);??

  18. ????????book.setAuthor("James");??

  19. ????????book.setCalendar(new?Date());??

  20. ????????book.setPrice(23.45f);???//默认是 0.0??

  21. ????????String?str?=?JaxbUtil.convertToXml(book);??

  22. ????????System.out.println(str);??

  23. ????}??

  24. ????/**?

  25. ?????*?@throws?JAXBException?

  26. ?????*/??

  27. ????@Test??

  28. ????public?void?showUnMarshaller()?{??

  29. ????????String?str?=?"<?xml?version=\"1.0\"?encoding=\"UTF-8\"?standalone=\"yes\"?>"?+??

  30. ????????????"<book?id=\"100\">"?+??

  31. ????????????"????<author>James</author>"?+??

  32. ?????????????"???<calendar>2013-03-29T09:25:56.004+08:00</calendar>"?+??

  33. ??????????????"??<price_1>23.45</price_1>"?+??

  34. ????????????"</book>";??

  35. ????????Book?book?=?JaxbUtil.converyToJavaBean(str,?Book.class);??

  36. ????????System.out.println(book);??

  37. ????}??

  38. }??


输出结果分别为:


<?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. import?javax.xml.bind.annotation.XmlAccessType;??

  3. import?javax.xml.bind.annotation.XmlAccessorType;??

  4. import?javax.xml.bind.annotation.XmlAttribute;??

  5. import?javax.xml.bind.annotation.XmlElement;??

  6. import?javax.xml.bind.annotation.XmlRootElement;??

  7. import?javax.xml.bind.annotation.XmlType;??

  8. /**?

  9. ?*?@author??????zhuc?

  10. ?*?@create??????2013-3-29?下午 2:51:44?

  11. ?*/??

  12. @XmlAccessorType(XmlAccessType.FIELD)??

  13. @XmlRootElement(name?=?"student")??

  14. @XmlType(propOrder?=?{})??

  15. public?class?Student?{??

  16. ????@XmlAttribute??

  17. ????private?Integer?id;??

  18. ????@XmlElement??

  19. ????private?String?name;??

  20. ????@XmlElement(name?=?"role")??

  21. ????private?Role?role;??

  22. ????/**?

  23. ?????*?@return?the?id?

  24. ?????*/??

  25. ????public?Integer?getId()?{??

  26. ????????return?id;??

  27. ????}??

  28. ????/**?

  29. ?????*?@return?the?name?

  30. ?????*/??

  31. ????public?String?getName()?{??

  32. ????????return?name;??

  33. ????}??

  34. ????/**?

  35. ?????*?@return?the?role?

  36. ?????*/??

  37. ????public?Role?getRole()?{??

  38. ????????return?role;??

  39. ????}??

  40. ????/**?

  41. ?????*?@param?id?the?id?to?set?

  42. ?????*/??

  43. ????public?void?setId(Integer?id)?{??

  44. ????????this.id?=?id;??

  45. ????}??

  46. ????/**?

  47. ?????*?@param?name?the?name?to?set?

  48. ?????*/??

  49. ????public?void?setName(String?name)?{??

  50. ????????this.name?=?name;??

  51. ????}??

  52. ????/**?

  53. ?????*?@param?role?the?role?to?set?

  54. ?????*/??

  55. ????public?void?setRole(Role?role)?{??

  56. ????????this.role?=?role;??

  57. ????}??

  58. ????/*?(non-Javadoc)?

  59. ?????*?@see?java.lang.Object#toString()?

  60. ?????*/??

  61. ????@Override??

  62. ????public?String?toString()?{??

  63. ????????return?"Student?[id="?+?id?+?",?name="?+?name?+?",?role="?+?role?+?"]";??

  64. ????}??

  65. }??


Java 代码??



  1. package?t2;??

  2. import?javax.xml.bind.annotation.XmlAccessType;??

  3. import?javax.xml.bind.annotation.XmlAccessorType;??

  4. import?javax.xml.bind.annotation.XmlElement;??

  5. import?javax.xml.bind.annotation.XmlType;??

  6. /**?

  7. ?*?@author??????zhuc?

  8. ?*?@create??????2013-3-29?下午 2:51:52?

  9. ?*/??

  10. @XmlAccessorType(XmlAccessType.FIELD)??

  11. @XmlType(propOrder?=?{?"name",?"desc"?})??

  12. public?class?Role?{??

  13. ????@XmlElement??

  14. ????private?String?name;??

  15. ????@XmlElement??

  16. ????private?String?desc;??

  17. ????/**?

  18. ?????*?@return?the?name?

  19. ?????*/??

  20. ????public?String?getName()?{??

  21. ????????return?name;??

  22. ????}??

  23. ????/**?

  24. ?????*?@return?the?desc?

  25. ?????*/??

  26. ????public?String?getDesc()?{??

  27. ????????return?desc;??

  28. ????}??

  29. ????/**?

  30. ?????*?@param?name?the?name?to?set?

  31. ?????*/??

  32. ????public?void?setName(String?name)?{??

  33. ????????this.name?=?name;??

  34. ????}??

  35. ????/**?

  36. ?????*?@param?desc?the?desc?to?set?

  37. ?????*/??

  38. ????public?void?setDesc(String?desc)?{??

  39. ????????this.desc?=?desc;??

  40. ????}??

  41. ????/*?(non-Javadoc)?

  42. ?????*?@see?java.lang.Object#toString()?

  43. ?????*/??

  44. ????@Override??

  45. ????public?String?toString()?{??

  46. ????????return?"Role?[name="?+?name?+?",?desc="?+?desc?+?"]";??

  47. ????}??

  48. }??


Java 代码??



  1. package?t2;??

  2. import?org.junit.Test;??

  3. import?utils.JaxbUtil;??

  4. /**?

  5. ?*?@author??????zhuc?

  6. ?*?@create??????2013-3-29?下午 2:52:00?

  7. ?*/??

  8. public?class?JaxbTest2?{??

  9. ????@Test??

  10. ????public?void?showMarshaller()?{??

  11. ????????Student?student?=?new?Student();??

  12. ????????student.setId(12);??

  13. ????????student.setName("test");??

  14. ????????Role?role?=?new?Role();??

  15. ????????role.setDesc("管理");??

  16. ????????role.setName("班长");??

  17. ????????student.setRole(role);??

  18. ????????String?str?=?JaxbUtil.convertToXml(student);??

  19. ????????System.out.println(str);??

  20. ????}??

  21. ????@Test??

  22. ????public?void?showUnMarshaller()?{??

  23. ????????String?str?=?"<?xml?version=\"1.0\"?encoding=\"UTF-8\"?standalone=\"yes\"?>"+??

  24. ????????????"<student?id=\"12\">"+??

  25. ????????????"????<name>test</name>"+??

  26. ?????????????"???<role>"+??

  27. ??????????????"??????<name>班长</name>"+??

  28. ???????????????"?????<desc>管理</desc>"+??

  29. ????????????????"</role>"+??

  30. ????????????"</student>";??

  31. ????????Student?student?=?JaxbUtil.converyToJavaBean(str,?Student.class);??

  32. ????????System.out.println(student);??

  33. ????}??

  34. }??


输出结果分别为:


<?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. import?java.util.List;??

  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.XmlElementWrapper;??

  7. import?javax.xml.bind.annotation.XmlRootElement;??

  8. import?javax.xml.bind.annotation.XmlType;??

  9. /**?

  10. ?*?@author??????zhuc?

  11. ?*?@create??????2013-3-29?下午 2:55:56?

  12. ?*/??

  13. @XmlAccessorType(XmlAccessType.FIELD)??

  14. @XmlRootElement(name?=?"country")??

  15. @XmlType(propOrder?=?{?"name",?"provinceList"?})??

  16. public?class?Country?{??

  17. ????@XmlElement(name?=?"country_name")??

  18. ????private?String?name;??

  19. ????@XmlElementWrapper(name?=?"provinces")??

  20. ????@XmlElement(name?=?"province")??

  21. ????private?List<Province>?provinceList;??

  22. ????/**?

  23. ?????*?@return?the?name?

  24. ?????*/??

  25. ????public?String?getName()?{??

  26. ????????return?name;??

  27. ????}??

  28. ????/**?

  29. ?????*?@return?the?provinceList?

  30. ?????*/??

  31. ????public?List<Province>?getProvinceList()?{??

  32. ????????return?provinceList;??

  33. ????}??

  34. ????/**?

  35. ?????*?@param?name?the?name?to?set?

  36. ?????*/??

  37. ????public?void?setName(String?name)?{??

  38. ????????this.name?=?name;??

  39. ????}??

  40. ????/**?

  41. ?????*?@param?provinceList?the?provinceList?to?set?

  42. ?????*/??

  43. ????public?void?setProvinceList(List<Province>?provinceList)?{??

  44. ????????this.provinceList?=?provinceList;??

  45. ????}??

  46. ????/*?(non-Javadoc)?

  47. ?????*?@see?java.lang.Object#toString()?

  48. ?????*/??

  49. ????@Override??

  50. ????public?String?toString()?{??

  51. ????????return?"Country?[name="?+?name?+?",?provinceList="?+?provinceList?+?"]";??

  52. ????}??

  53. }??


Java 代码??



  1. package?t3;??

  2. import?javax.xml.bind.annotation.XmlAccessType;??

  3. import?javax.xml.bind.annotation.XmlAccessorType;??

  4. import?javax.xml.bind.annotation.XmlElement;??

  5. import?javax.xml.bind.annotation.XmlType;??

  6. /**?

  7. ?*?@author??????zhuc?

  8. ?*?@create??????2013-3-29?下午 2:56:03?

  9. ?*/??

  10. @XmlAccessorType(XmlAccessType.FIELD)??

  11. @XmlType(propOrder?=?{?"name",?"provCity"?})??

  12. public?class?Province?{??

  13. ????@XmlElement(name?=?"province_name")??

  14. ????private?String?name;??

  15. ????@XmlElement(name?=?"prov_city")??

  16. ????private?String?provCity;??

  17. ????/**?

  18. ?????*?@return?the?provCity?

  19. ?????*/??

  20. ????public?String?getProvCity()?{??

  21. ????????return?provCity;??

  22. ????}??

  23. ????/**?

  24. ?????*?@param?provCity?the?provCity?to?set?

  25. ?????*/??

  26. ????public?void?setProvCity(String?provCity)?{??

  27. ????????this.provCity?=?provCity;??

  28. ????}??

  29. ????/**?

  30. ?????*?@return?the?name?

  31. ?????*/??

  32. ????public?String?getName()?{??

  33. ????????return?name;??

  34. ????}??

  35. ????/**?

  36. ?????*?@param?name?the?name?to?set?

  37. ?????*/??

  38. ????public?void?setName(String?name)?{??

  39. ????????this.name?=?name;??

  40. ????}??

评论

发布
暂无评论
Jaxb2 实现JavaBean与xml互转