用xstream解析XML

前端之家收集整理的这篇文章主要介绍了用xstream解析XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. public class WeBoSquareHandler {
  2. public static void main(String[] args) {
  3. parseXML(getXML());
  4. }
  5.  
  6. public static void parseXML(String xmls) {
  7.  
  8. XStream xstream = new XStream();
  9. // 设置别名
  10. xstream.alias("root",WeboSquare.class);
  11. xstream.alias("topic",Topic.class);
  12. xstream.alias("image",Image.class);
  13. xstream.alias("attach",Attach.class);
  14. xstream.alias("video",Video.class);
  15. xstream.alias("music",Music.class);
  16. xstream.alias("parent",Parent.class);
  17.  
  18. // 忽略下列的名字,要用addImplicitCollection()带三个参数的方法,带两个参数的方法会乱了
  19. xstream.addImplicitCollection(Topic.class,"attachs",Attach.class);
  20. xstream.addImplicitCollection(Topic.class,"images",Image.class);
  21. xstream.addImplicitCollection(Topic.class,"videos",Video.class);
  22. xstream.addImplicitCollection(Topic.class,"musics",Music.class);
  23.  
  24. // 设置属性
  25. xstream.useAttributeFor(WeboSquare.class,"status");
  26. xstream.useAttributeFor(WeboSquare.class,"code");
  27.  
  28. System.out.println("\n将xml转换为Java对象!");
  29. WeboSquare weboSquare = (WeboSquare) xstream.fromXML(xmls);
  30. System.out.println(weboSquare.toString());
  31. }
  32.  
  33. public static String getXML() {
  34. String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  35. + "<root status=\"0\" code=\"200\">" + "<total>1821</total>"
  36. + "<max_tid>2043</max_tid>" + "<uid>1</uid>"
  37. + "<count>20</count>" + "<topic>" + "<tid>2065</tid>"
  38. + "<uid>1</uid>" + "<username>admin</username>"
  39. + "<nickname>微博管理员</nickname>"
  40. + "<face>http://t2.hq.ctc.com/images/face/b/43/1_s.jpg</face>"
  41. + "<content>本微博包含了图片,视频,音乐和附件</content>"
  42. + "<validate>0</validate>" + "<imageid>266</imageid>"
  43. + "<videoid>7</videoid>" + "<musicid>6</musicid>"
  44. + "<attachid>3,4,5</attachid>" + "<image>" + "<id>266</id>"
  45. + "<small>{site_url}/images/topic/3/21/266_s.jpg</small>"
  46. + "<original>{site_url}/images/topic/3/21/266_o.jpg</original>"
  47. + "</image>" + "<attach>" + "<id>3</id>"
  48. + "<attach_img>images/filetype/xls.gif</attach_img>"
  49. + "<attach_name>jxwb_syds_2013060412 (5).xls</attach_name>"
  50. + "<attach_score>0</attach_score>"
  51. + "<attach_down>0</attach_down>"
  52. + "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
  53. + "<attach>" + "<id>4</id>"
  54. + "<attach_img>images/filetype/xls.gif</attach_img>"
  55. + "<attach_name>jxwb_syds_2013060412 (4).xls</attach_name>"
  56. + "<attach_score>0</attach_score>"
  57. + "<attach_down>0</attach_down>"
  58. + "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
  59. + "<attach>" + "<id>5</id>"
  60. + "<attach_img>images/filetype/xls.gif</attach_img>"
  61. + "<attach_name>jxwb_syds_2013060412 (3).xls</attach_name>"
  62. + "<attach_score>0</attach_score>"
  63. + "<attach_down>0</attach_down>"
  64. + "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
  65. + "<video>" + "<video_hosts>sina.com.cn</video_hosts>"
  66. + "<video_url>{site_url}/…/142462517543.html</video_url>"
  67. + "<video_img>{site_url}/images/211529932731.jpg</video_img>"
  68. + "</video>" + "<music>" + "<music_id>2</music_id>"
  69. + "<music_url>1769023170</music_url>" + "</music>"
  70. + "<replys>0</replys>" + "<forwards>0</forwards>"
  71. + "<type>reply</type>" + "<dateline>06月06日 18时28分</dateline>"
  72. + "<from_string>来自新浪微博</from_string>"
  73. + "<longtextid>11</longtextid>" + "<parent>"
  74. + "<tid>2064</tid>" + "<uid>1</uid>"
  75. + "<username>admin</username>" + "<nickname>微博管理员</nickname>"
  76. + "<face>http://t2.hq.ctc.com/images/face/b/43/1_s.jpg</face>"
  77. + "<content>音乐和视频</content>" + "<validate>0</validate>"
  78. + "<imageid>265</imageid>" + "<videoid>6</videoid>"
  79. + "<musicid>2</musicid>" + "<attachid>2</attachid>"
  80. + "<replys>1</replys>" + "<forwards>0</forwards>"
  81. + "<type>first</type>" + "<addtime>06月06日 18时01分</addtime>"
  82. + "<from_string>来自新浪微博</from_string>" + "<image>"
  83. + "<id>265</id>"
  84. + "<small>{site_url}/images/topic/e/53/265_s.jpg</small>"
  85. + "<original>aaa/images/topic/e/53/265_o.jpg</original>"
  86. + "</image>" + "<attach>" + "<id>2</id>"
  87. + "<attach_img>images/filetype/xls.gif</attach_img>"
  88. + "<attach_name>jxwb_syds_2013060412 (5).xls</attach_name>"
  89. + "<attach_score>0</attach_score>"
  90. + "<attach_down>0</attach_down>"
  91. + "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
  92. + "<video>" + "<video_hosts>sina.com.cn</video_hosts>"
  93. + "<video_url>{site_url}/…/142462517543.html</video_url>"
  94. + "<video_img>{site_url}/images/211529932731.jpg</video_img>"
  95. + "</video>" + "<music>" + "<music_id>2</music_id>"
  96. + "<music_url>1769023170</music_url>" + "</music>"
  97. + "</parent>" + "</topic>" + "</root>";
  98. return xml;
  99. }
  100.  
  101. }

猜你在找的XML相关文章