本人大四即将毕业的准程序员(JavaSE、JavaEE、android等)一枚,小项目也做过一点,于是乎一时兴起就写了一些工具。
我会在本博客中陆续发布一些平时可能会用到的工具。
代码质量可能不是很好,大家多担待!
代码或者思路有不妥之处,还希望大牛们能不吝赐教哈!
以下代码为本人原创,转载请注明:
本文转载,来自:http://www.cnblogs.com/tiantianbyconan/archive/2013/02/19/2917398.html
JOXMLBuilder工具类:一键把多个域对象数据转换为XML格式数据,方便用于数据的传输和交互。功能类似于通过Gson来生成Json数据。
源码如下:
View Code
使用方法如下:
例如:
Student类(该类有属性name,age,isBoy,books等属性;其中books属性是一个List,存放Book对象):
1 private String name; 2 private int age; 3 private boolean isBoy; 4 private List<Book> books; 5 //并实现getter和setter方法;
Book类(该类有属性name,author,number,length,width,isBorrowed等属性):
1 private String name; 2 private String author; 3 private int number; 4 private float length; 5 private float width; 6 private boolean isBorrowed; 7 //并实现getter和setter方法;
现在有一个List<Student>类型的数据,通过以下代码把该List转换为xml:
1 List<Student> list = new ArrayList<Student>(); 2 3 //构建几个Student对象,放入list中 4 //…… 5 6 //完整数据版(不使用includes和excludes) 7 JOXMLBuilder jOXMLBuilder = new JOXMLBuilder(list); 8 jOXMLBuilder.xmlBuild().toString(); 9 10 //或者使用包括/排除: 11 JOXMLBuilder jOXMLBuilder = new JOXMLBuilder(list, new String[]{"name", "age"}, null); 12 jOXMLBuilder.xmlBuild().toString(); 13 14 //或者使用方法链风格: 15 new JOXMLBuilder().setExcludes("name", "age").xmlBuild().toString();
转换之后的xml(完整数据版(不使用includes和excludes)):
1 <?xml version="1.0" encoding="utf-8"?> 2 <StudentAll> 3 <Student> 4 <name>hello</name> 5 <age>23</age> 6 <isBoy>true</isBoy> 7 <BookAll> 8 <Book> 9 <name>book1</name> 10 <author>author1</author> 11 <number>123</number> 12 <length>23.5</length> 13 <width>18.0</width> 14 <isBorrowed>true</isBorrowed> 15 </Book> 16 <Book> 17 <name>book2</name> 18 <author>author2</author> 19 <number>43</number> 20 <length>42.23</length> 21 <width>30.57</width> 22 <isBorrowed>false</isBorrowed> 23 </Book> 24 </BookAll> 25 </Student> 26 27 <Student> 28 <name>world</name> 29 <age>22</age> 30 <isBoy>false</isBoy> 31 <BookAll> 32 <Book> 33 <name>book1</name> 34 <author>author1</author> 35 <number>123</number> 36 <length>23.5</length> 37 <width>18.0</width> 38 <isBorrowed>true</isBorrowed> 39 </Book> 40 <Book> 41 <name>book3</name> 42 <author>author3</author> 43 <number>875</number> 44 <length>20.59</length> 45 <width>15.08</width> 46 <isBorrowed>false</isBorrowed> 47 </Book> 48 <Book> 49 <name>book4</name> 50 <author>author4</author> 51 <number>165</number> 52 <length>22.75</length> 53 <width>19.61</width> 54 <isBorrowed>true</isBorrowed> 55 </Book> 56 </BookAll> 57 </Student> 58 </StudentAll>
本文转自天天_byconan博客园博客,原文链接:http://www.cnblogs.com/tiantianbyconan/archive/2013/02/19/2917398.html,如需转载请自行联系原作者