xml和java Bean的直接转换,Xmap

前端之家收集整理的这篇文章主要介绍了xml和java Bean的直接转换,Xmap前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
[java] view plain copy
  1. packagetest;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importorg.nuxeo.common.xmap.annotation.XNode;
  5. importorg.nuxeo.common.xmap.annotation.XNodeList;
  6. importorg.nuxeo.common.xmap.annotation.XObject;
  7. /**
  8. *Book实体对象,此处用XMap注解
  9. *@authorAdministrator
  10. *
  11. */
  12. @XObject
  13. publicclassBook{
  14. @XNode("title")
  15. privateStringtitle;
  16. @XNode("@no")
  17. privateStringno;
  18. @XNodeList(value="subject",type=ArrayList.class,componentType=String.class)
  19. privateList<String>subjects;
  20. @XNode("price")
  21. privateintprice;
  22. publicStringgetTitle(){
  23. returntitle;
  24. }
  25. voidsetTitle(Stringtitle){
  26. this.title=title;
  27. publicList<String>getSubjects(){
  28. returnsubjects;
  29. voidsetSubjects(List<String>subjects){
  30. this.subjects=subjects;
  31. intgetPrice(){
  32. returnprice;
  33. voidsetPrice(intprice){
  34. this.price=price;
  35. publicStringgetNo(){
  36. returnno;
  37. voidsetNo(Stringno){
  38. this.no=no;
  39. }
  40. /**==================================================**/
  41. packagetest;
  42. importjava.util.ArrayList;
  43. importjava.util.List;
  44. /**
  45. *图书对象集合类
  46. *@authorzhouyu
  47. *
  48. */
  49. @XObject(value="books")
  50. classBooks{
  51. @XNodeList(value="book",componentType=Book.class)
  52. privateList<Book>book;
  53. publicList<Book>getBook(){
  54. returnbook;
  55. voidsetBook(List<Book>book){
  56. this.book=book;
  57. voidaddBook(Bookb){
  58. if(book==null){
  59. book=newArrayList<Book>();
  60. book.add(b);
  61. /**==================================================**/
  62. importorg.nuxeo.common.xmap.XMap;
  63. importcom.io.IoUtils;
  64. *从对象到xml转换
  65. *@authorzhouyu
  66. classObjectToXmlDemo{
  67. staticvoidmain(String[]args){
  68. try{
  69. //声明一组书的根节点
  70. Booksbooks=newBooks();
  71. //声明一本书
  72. Bookbook=newBook();
  73. //书的类型
  74. List<String>sj=newArrayList<String>();
  75. sj.add("java");
  76. sj.add("xml");
  77. //设置书节点的属性
  78. book.setPrice(10);
  79. book.setTitle("java是傻逼");
  80. book.setSubjects(sj);
  81. book.setNo("1");
  82. //将书节点添加到跟节点
  83. books.addBook(book);
  84. //声明一本书
  85. Bookbook2=newBook();
  86. //书的类型
  87. List<String>sj2=newArrayList<String>();
  88. sj2.add("C++");
  89. sj2.add("c");
  90. //设置书节点的属性
  91. book2.setPrice(10);
  92. book2.setTitle("c++程序设计");
  93. book2.setSubjects(sj2);
  94. book2.setNo("2");
  95. //将书节点添加到跟节点
  96. books.addBook(book2);
  97. XMapxmp=newXMap();
  98. xmp.register(Books.class);
  99. List<String>filters= Stringcontent=xmp.asXmlString(books,"UTF-8",filters,true);
  100. IoUtils.WriteFile(content,"books.xml");
  101. }catch(Exceptione){
  102. e.printStackTrace();
  103. importjava.io.File;
  104. importjava.io.FileInputStream;
  105. importjava.io.InputStream;
  106. importjava.net.URL;
  107. importorg.nuxeo.common.xmap.XMap;
  108. classXmlToObjectDemo{
  109. voidmain(String[]args){
  110. try{
  111. //URLurl=newFile("books.xml").toURL();
  112. InputStreamin=newFileInputStream(newFile("books.xml"));
  113. Object[]result=(Object[])xmp.loadAll(in);
  114. Booksbooks=(Books)result[0];
  115. for(inti=0;i<books.getBook().size();i++)
  116. {
  117. Bookbook=books.getBook().get(i);
  118. System.out.println(book.getNo());
  119. System.out.println(book.getPrice());
  120. System.out.println(book.getTitle());
  121. System.out.println(book.getSubjects().get(i));
  122. //TODOAuto-generatedcatchblock
  123. e.printStackTrace();
  124. <?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
  125. <books>
  126. <bookno="1">
  127. <title>java是傻逼</title>
  128. <subject>java</subject>
  129. <subject>xml</subject>
  130. <price>10</price>
  131. </book>
  132. <bookno="2">
  133. <title>c++程序设计</title>
  134. <subject>C++</subject>
  135. <subject>c</subject>
  136. </books>
本文转自:http://blog.csdn.net/xiaoyu411502/article/details/5566398
原文链接:/xml/298217.html

猜你在找的XML相关文章