使⽤JAXB 实现JAVA 解析XML (节点⼤⼩写和属性的互相转换
实例
输出如下:01. package st; 02. 03. 04. 05. import java.util.ArrayList; 06. import java.util.List; 07. 08. import st.JaxbUtil.CollectionWrapper; 09. 10. 11. public class Test { 12. 13. /** 14. * @param args 15. */ 16. public static void main(String[] args) { 17. 18. //创建java 对象 19. 20. Hotel hotel=new Hotel(); 21. hotel.setId(1); 22. hotel.setName("name1"); 23. 24. RoomTypeVO t1=new RoomTypeVO(); 25. t1.setPrice("20"); 26. t1.setTypeid(1); 27. t1.setTypename("typename1"); 28. 29. RoomTypeVO t2=new RoomTypeVO(); 30. t2.setPrice("30"); 31. t2.setTypeid(2); 32. t2.setTypename("typename2"); 33. 34. 35. List<RoomTypeVO> RoomTypeVOs=new ArrayList<RoomTypeVO>(); 36. RoomTypeVOs.add(t1); 37. RoomTypeVOs.add(t2); 38. hotel.setRoomTypeVOs(RoomTypeVOs); 39. 40. 41. //将java 对象转换为XML 字符串 42. JaxbUtil requestBinder = new JaxbUtil(Hotel.class , 43. CollectionWrapper.class ); 44. String retXml = Xml(hotel, "utf-8"); 45. System.out.println("xml:"+retXml); 46. 47. //将xml 字符串转换为java 对象 48. JaxbUtil resultBinder = new JaxbUtil(Hotel.class , 49. CollectionWrapper.class ); 50. Hotel hotelObj = resultBinder.fromXml(retXml); 51.
52. 53. 54. System.out.println("hotelid:"+Id()); 55. System.out.println("hotelname:"+Name()); 56. for (RoomTypeVO RoomTypeVOs()) 57. { 58. System.out.println("Typeid:"+Typeid()); 59. System.out.println("Typename:"+Typename()); 60. } 61. 62. 63. } 64. 65. }
相关类:[html]
01. xml:<?xml version ="1.0" encoding ="utf-8" standalone ="yes"?> 02. <hotel id ="1"> 03. <name>name1</name> 04. <RoomTypeVOs> 05. <RoomTypeVO typeid ="1"> 06. <price>20</price> 07. <typename>typename1</typename> 08. </RoomTypeVO> 09. <RoomTypeVO typeid ="2"> 10. <price>30</price> 11. <typename>typename2</typename> 12. </RoomTypeVO> 13. </RoomTypeVOs> 14. </hotel> 15. 16. 17. hotelid:1 18. hotelname:name1 19. Typeid:1 20. Typename:typename1 21. Typeid:2 22. Typename:typename2 [java]
01. package st; 02. 03. 04. 05. import java.util.List; 06. 07. import l.bind.a
nnotation.XmlAttribute; 08. import l.bind.annotation.XmlElement; 09. import l.bind.annotation.XmlElementWrapper; 10. import l.bind.annotation.XmlRootElement; 11. 12. @XmlRootElement (name = "hotel") 13. public class Hotel { 14. @XmlElementWrapper (name = "RoomTypeVOs") 15. @XmlElement (name = "RoomTypeVO") 16. public List<RoomTypeVO> getRoomTypeVOs() { 17. return RoomTypeVOs; 18. } 19. public void setRoomTypeVOs(List<RoomTypeVO> roomTypeVOs) { 20. RoomTypeVOs = roomTypeVOs; 21. } 22. private List<RoomTypeVO> RoomTypeVOs; 23. 24. /@XmlElement(name = "id") 25. @XmlAttribute (name = "id") 26. public int getId() { 27. return id; 28. } 29. public void setId(int id) { 30. this .id = id; 31. } 32. @XmlElement (name = "name") 33. public String getName() { 34. return name; 35. } 36. public void setName(String name) { 37. this .name = name; 38. } 39. private int id; 40. private String name; 41. 42. 43. } 44. 45. 46. 47. package st; 48. 49. import l.bind.annotation.XmlAttribute;
jaxb 简单⼯具类:49. import l.bind.annotation.XmlAttribute; 50. import l.bind.annotation.XmlElement; 51. 52. public class RoomTypeVO { 53. 54. ///@XmlElement(name = "typeid") 55. @XmlAttribute (name = "typeid") 56. public int getTypeid
() { 57. return typeid; 58. } 59. public void setTypeid(int typeid) { 60. this .typeid = typeid; 61. } 62. @XmlElement (name = "typename") 63. public String getTypename() { 64. return typename; 65. } 66. public void setTypename(String typename) { 67. this .typename = typename; 68. } 69. @XmlElement (name = "price") 70. public String getPrice() { 71. return price; 72. } 73. public void setPrice(String price) { 74. this .price = price; 75. } 76. private int typeid; 77. private String typename; 78. private String price; 79. 80. } [java]
01. package st; 02. 03. import java.io.StringReader; 04. import java.io.StringWriter; 05. import java.util.Collection; 06. 07. import l.bind.JAXBContext; 08. import l.bind.JAXBElement; 09. import l.bind.JAXBException; 10. import l.bind.Marshaller; 11. import l.bind.Unmarshaller; 12. import l.bind.annotation.XmlAnyElement; 13. import l.namespace.QName; 14. 15. import org.apachemons.lang.StringUtils; 16. 17. /** 18. * 使⽤Jaxb2.0实现XML<->Java Object 的Binder. 19. * 20. * 特别⽀持Root 对象是List 的情形. 21. * 22. * @author 23. */ 24. public class JaxbUtil { 25. // 多线程安全的Context. 26. private JAXBContext jaxbContext; 27. 28. /** 29. * @param types 30. * 所有需要序列化的Root 对象的类型. 31. */ 32.
public JaxbUtil(Class<?>... types) { 33. try { 34. jaxbContext = wInstance(types); 35. } catch (JAXBException e) { 36. throw new RuntimeException(e); 37. } 38. } 39. 40. /** 41. * Java Object->Xml. 42. */
42. */
43. public String toXml(Object root, String encoding) {
44. try {
45. StringWriter writer = new StringWriter();
46. createMarshaller(encoding).marshal(root, writer);
47. String();
48. } catch (JAXBException e) {
49. throw new RuntimeException(e);
50. }
51. }
52.
53. /**
54. * Java Object->Xml, 特别⽀持对Root Element是Collection的情形.
55. */
java xml是什么56. @SuppressWarnings("unchecked")
57. public String toXml(Collection root, String rootName, String encoding) {
58. try {
59. CollectionWrapper wrapper = new CollectionWrapper();
60. llection = root;
61.
62. JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(
63. new QName(rootName), CollectionWrapper.class, wrapper);
64.
65. StringWriter writer = new StringWriter();
66. createMarshaller(encoding).marshal(wrapperElement, writer);
67.
68. String();
69. } catch (JAXBException e) {
70. throw new RuntimeException(e);
71. }
72. }
73.
74. /**
75. * Xml->Java Object.
76. */
77. @SuppressWarnings("unchecked")
78. public <T> T fromXml(String xml) {
79. try {
80. StringReader reader = new StringReader(xml);
81. return (T) createUnmarshaller().unmarshal(reader);
82. } catch (JAXBException e) {
83. throw new RuntimeException(e);
84. }
85. }
86.
87. /**
88. * Xml->Java Object, ⽀持⼤⼩写敏感或不敏感.
89. */
90. @SuppressWarnings("unchecked")
91. public <T> T fromXml(String xml, boolean caseSensitive) {
92. try {
93. String fromXml = xml;
94. if (!caseSensitive)
95. fromXml = LowerCase();
96. StringReader reader = new StringReader(fromXml);
97. return (T) createUnmarshaller().unmarshal(reader);
98. } catch (JAXBException e) {
99. throw new RuntimeException(e);
100. }
101. }
102.
103. /**
104. * 创建Marshaller, 设定encoding(可为Null).
105. */
106. public Marshaller createMarshaller(String encoding) {
107. try {
108. Marshaller marshaller = ateMarshaller();
109.
110. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
111.
112. if (StringUtils.isNotBlank(encoding)) {
113. marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
114. }
115. return marshaller;
116. } catch (JAXBException e) {
117. throw new RuntimeException(e);
118. }
119. }
120.
121. /**
121. /**
122. * 创建UnMarshaller.
123. */
124. public Unmarshaller createUnmarshaller() { 125. try {
126. ateUnmarshaller(); 127. } catch (JAXBException e) {
128. throw new RuntimeException(e); 129. }
130. }
131.
132. /**
133. * 封装Root Element 是 Collection的情况. 134. */
135. public static class CollectionWrapper { 136. @SuppressWarnings("unchecked") 137. @XmlAnyElement
138. protected Collection collection;
139. }
140. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论