Simple XML解析XML

前端之家收集整理的这篇文章主要介绍了Simple XML解析XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Simple XML解析XML

源数据如下:

  1. <RSS version="2.0">
  2. <channel>
  3.  
  4. <item>
  5. <newsid>321993</newsid>
  6. <title><![CDATA[德国骨科之力:和泉纱雾手办开订,买手办送角色CV写真]]></title>
  7. <v>000</v>
  8. <url><![CDATA[/html/it/321993.htm]]></url>
  9. <postdate>2017-8-19 12:40:45</postdate>
  10. <image>http://img.ithome.com/newsuploadfiles/thumbnail/2017/8/321993.jpg</image>
  11. <description><![CDATA[近日,A-1改编的四月番《情色漫画老师》女主角和泉纱雾最新手办开订,其中,豪华版还将附赠和泉纱雾声优藤田茜的写真集,以及原作者伏见司签名的明信片和电]]></description>
  12. <hitcount>805</hitcount>
  13. <commentcount>28</commentcount>
  14. <forbidcomment>false</forbidcomment>
  15. <cid>32</cid>
  16. </item>
  17. <item>
  18. <newsid>321992</newsid>
  19. <title><![CDATA[三星“弃疗”:Note 8旗舰机现身官网,外观一览无余]]></title>
  20. <v>000</v>
  21. <url><![CDATA[/html/android/321992.htm]]></url>
  22. <postdate>2017-8-19 12:38:42</postdate>
  23. <image>http://img.ithome.com/newsuploadfiles/thumbnail/2017/8/321992.jpg</image>
  24. <description><![CDATA[从外观到配置以及价格,三星即将在8月23日正式发布的下半年旗舰Note 8基本上已经没有秘密可言,但意外的是三星在发布会之前自己也做了一把爆料者]]></description>
  25. <hitcount>3820</hitcount>
  26. <commentcount>84</commentcount>
  27. <forbidcomment>false</forbidcomment>
  28. <cid>74</cid>
  29. </item>
  30. <item>
  31. <newsid>321991</newsid>
  32. <title><![CDATA[高通高层:一定能赢下和苹果的专利诉讼]]></title>
  33. <v>000</v>
  34. <url><![CDATA[/html/it/321991.htm]]></url>
  35. <postdate>2017-8-19 12:35:02</postdate>
  36. <image>http://img.ithome.com/newsuploadfiles/thumbnail/2017/8/321991.jpg</image>
  37. <description><![CDATA[芯片巨头高通和苹果的专利侵权诉讼已经持续了好几个月,目前看起来也远未到结束的时候。不过,近日该公司执行副总裁兼总法律顾问Donald J. Rosenberg强调,高通一定会赢]]></description>
  38. <hitcount>402</hitcount>
  39. <commentcount>20</commentcount>
  40. <forbidcomment>false</forbidcomment>
  41. <cid>150</cid>
  42. </item>
  43. <item>
  44. <newsid>321990</newsid>
  45. <title><![CDATA[vivo X20通过3C认证:支持18W快充]]></title>
  46. <v>000</v>
  47. <url><![CDATA[/html/android/321990.htm]]></url>
  48. <postdate>2017-8-19 12:13:23</postdate>
  49. <image>http://img.ithome.com/newsuploadfiles/thumbnail/2017/8/321990.jpg</image>
  50. <description><![CDATA[vivo X9s、X9s Plus之后,vivo新旗舰已经在路上了。近日,两款型号为X20和X20A的vivo新机已通过3C认证,从相关信息来看,这两款机型支持最大18W快充]]></description>
  51. <hitcount>2814</hitcount>
  52. <commentcount>50</commentcount>
  53. <forbidcomment>false</forbidcomment>
  54. <cid>74</cid>
  55. </item>
  56. <item>
  57. <newsid>321973</newsid>
  58. <title><![CDATA[量变的8代Core:更多Intel Coffee Lake处理器规格曝光]]></title>
  59. <v>000</v>
  60. <url><![CDATA[/html/digi/321973.htm]]></url>
  61. <postdate>2017-8-19 10:49:35</postdate>
  62. <image>http://img.ithome.com/newsuploadfiles/thumbnail/2017/8/321973.jpg</image>
  63. <description><![CDATA[在昨天我们报道了Coffee Lake的性能提升幅度PPT,但由于照片太模糊,部分参数无法看清,本着对读者负责任的态度,干脆只给出型号和单/多线程性能提升的资料]]></description>
  64. <hitcount>6030</hitcount>
  65. <commentcount>125</commentcount>
  66. <forbidcomment>false</forbidcomment>
  67. <cid>100</cid>
  68. </item>
  69.  
  70. </channel>
  71. </RSS>

开始解析

可以看到源数据分为3层,我分别以ScienceRSS,ScienceChannel,ScienceNews来名称

  • 第一层是一个对象
  • 第二层是一个List
  • 第三层是一个对象

第一层

  1. @Root(name = "RSS",strict = false)
  2. public class ScienceRSS {
  3. //里面有一个version,如果不加strict = false就会报错
  4.  
  5. @Element(name = "channel")
  6. public ScienceChannel scienceChannel;
  7. }

第二层

  1. @Root(name = "channel") //根元素
  2. public class ScienceChannel {
  3.  
  4. @ElementList(inline = true,required = false) //里面是数组
  5. public List<ScienceNews> mScienceNewsList;
  6.  
  7. public List<ScienceNews> getmScienceNewsList() {
  8. return mScienceNewsList;
  9. }
  10.  
  11. public void setmScienceNewsList(List<ScienceNews> mScienceNewsList) {
  12. this.mScienceNewsList = mScienceNewsList;
  13. }
  14. }

第三层

  1. @Root(name = "item",strict = false)
  2. public class ScienceNews {
  3.  
  4. /**
  5. * 新闻id
  6. */
  7. @Element(name = "newsid")
  8. public String newsId;
  9. /**
  10. * 新闻标题
  11. */
  12. @Element(name = "title")
  13. public String title;
  14. /**
  15. * 新闻的url(不完整)
  16. */
  17. @Element(name = "url")
  18. public String url;
  19. /**
  20. * 更新时间
  21. */
  22. @Element(name = "postdate")
  23. public String postdate;
  24. /**
  25. * 图片地址
  26. */
  27. @Element(name = "image")
  28. public String image;
  29. /**
  30. * 描述
  31. */
  32. @Element(name = "description")
  33. public String description;
  34. /**
  35. * 点击数量
  36. */
  37. @Element(name = "hitcount")
  38. public String hitCount;
  39. /**
  40. * 评论数量
  41. */
  42. @Element(name = "commentcount")
  43. public String commentCount;
  44. /**
  45. * 禁止评论? false true
  46. */
  47. @Element(name = "forbidcomment")
  48. public boolean forbidComment;
  49. @Element(name = "cid")
  50. public String cid;
  51. }

正式开始用Simple XML进行解析

  1. Persister persister = new Persister();
  2. ScienceRSS scienceRSS = persister.read(ScienceRSS.class,result);
  3. ScienceChannel scienceChannel = scienceRSS.scienceChannel;
  4. return scienceChannel.getmScienceNewsList();

怎么样,是不是非常简单啊.不过现在用XML是真的少,一般都用JSON.了解一下还是好的.

猜你在找的XML相关文章