xml与bean间相互转换(补充)

前端之家收集整理的这篇文章主要介绍了xml与bean间相互转换(补充)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
今天x被stream对xmlnode的属性(attribute)解析的问题一直困扰着,查询了很久都告知我要手写一个Converter,那岂不意味着我每解析一个xml文件,就得写一次Converter,那样太脑残了,最后搜索到其实可以用注解解决这个问题
XStream常用注解 用法
@XStreamOmitField 指定该属性为一个节点
@XStreamAsAttribute 指定该属性为一个节点的一个属性
@XStreamAlias("alias") 指定该属性在xml文件中对应的节点或属性名称为alias

@XStreamImplicit(itemFieldName="alias") 指定同一标签下多个同名元素的显示为alias


待解析的xml字串:

<user uname="evon">
<utype tid="type">
<tpoint>
<ux>100</ux>
<uy>100</uy>
</tpoint>
<tpoint>
<ux>200</ux>
<uy>200</uy>
</tpoint>
</utype>
<upoint>
<ux>0</ux>
<uy>0</uy>
</upoint>
</user>


用XStream解析,节点属性及子节点均使用注解,特别注意的是:在使用注解的时候一定要在使用前开启注解解析

  1. xStream.autodetectAnnotations(true);

以下是解析的例子:

  1. package org.evon.example;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.thoughtworks.xstream.XStream;
  7. import com.thoughtworks.xstream.annotations.XStreamAlias;
  8. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  9. import com.thoughtworks.xstream.annotations.XStreamImplicit;
  10. import com.thoughtworks.xstream.annotations.XStreamOmitField;
  11. import com.thoughtworks.xstream.io.xml.DomDriver;
  12.  
  13. public class XmlBean {
  14. public static void main(String[] args) {
  15. User user = createUser();
  16. XStream xStream = new XStream(new DomDriver());
  17. xStream.autodetectAnnotations(true);
  18. String xml = xStream.toXML(user);
  19. System.out.println("bean转换成的xml:");
  20. System.out.println(xml);
  21. User newUser = (User)xStream.fromXML(xml);
  22. System.out.println("xml转换成的bean,bean重新转换成的xml:");
  23. System.out.println(xStream.toXML(newUser));
  24. }
  25. private static User createUser(){
  26. Type type = new Type();
  27. type.setId("type");
  28. List<Point> pointList = new ArrayList<Point>();
  29. Point point1 = new Point();
  30. point1.setX(100);
  31. point1.setY(100);
  32. Point point2 = new Point();
  33. point2.setX(200);
  34. point2.setY(200);
  35. pointList.add(point1);
  36. pointList.add(point2);
  37. type.setPointList(pointList);
  38. Point point = new Point();
  39. point.setX(0);
  40. point.setY(0);
  41. User user = new User();
  42. user.setUid(8888);
  43. user.setUname("evon");
  44. user.setType(type);
  45. user.setPoint(point);
  46. return user;
  47. }
  48. }
  49.  
  50. @XStreamAlias("user")
  51. class User {
  52. @XStreamOmitField
  53. @XStreamAlias("uid")
  54. private int uid;
  55. @XStreamAsAttribute
  56. @XStreamAlias("uname")
  57. private String uname;
  58. @XStreamAlias("utype")
  59. private Type type;
  60. @XStreamAlias("upoint")
  61. private Point point;
  62. public int getUid() {
  63. return uid;
  64. }
  65. public void setUid(int uid) {
  66. this.uid = uid;
  67. }
  68. public String getUname() {
  69. return uname;
  70. }
  71. public void setUname(String uname) {
  72. this.uname = uname;
  73. }
  74. public Type getType() {
  75. return type;
  76. }
  77. public void setType(Type type) {
  78. this.type = type;
  79. }
  80. public Point getPoint() {
  81. return point;
  82. }
  83. public void setPoint(Point point) {
  84. this.point = point;
  85. }
  86. }
  87.  
  88. @XStreamAlias("utype")
  89. class Type{
  90. @XStreamAsAttribute
  91. @XStreamAlias("tid")
  92. private String id;
  93. @XStreamImplicit(itemFieldName="tpoint")
  94. private List<Point> pointList;
  95. public String getId() {
  96. return id;
  97. }
  98. public void setId(String id) {
  99. this.id = id;
  100. }
  101. public List<Point> getPointList() {
  102. return pointList;
  103. }
  104. public void setPointList(List<Point> pointList) {
  105. this.pointList = pointList;
  106. }
  107. }
  108.  
  109. @XStreamAlias("upoint")
  110. class Point{
  111. @XStreamAlias("ux")
  112. private int x;
  113. @XStreamAlias("uy")
  114. private int y;
  115. public int getX() {
  116. return x;
  117. }
  118. public void setX(int x) {
  119. this.x = x;
  120. }
  121. public int getY() {
  122. return y;
  123. }
  124. public void setY(int y) {
  125. this.y = y;
  126. }
  127. }

猜你在找的XML相关文章