Simple XML解析XML

25 阅读1分钟

false

74

321991

<![CDATA[高通高层:一定能赢下和苹果的专利诉讼]]>

000

2017-8-19 12:35:02

img.ithome.com/newsuploadf…

402

20

false

150

321990

<![CDATA[vivo X20通过3C认证:支持18W快充]]>

000

2017-8-19 12:13:23

img.ithome.com/newsuploadf…

2814

50

false

74

321973

<![CDATA[量变的8代Core:更多Intel Coffee Lake处理器规格曝光]]>

000

2017-8-19 10:49:35

img.ithome.com/newsuploadf…

6030

125

false

100

开始解析

====

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

  • 第一层是一个对象

  • 第二层是一个List

  • 第三层是一个对象

第一层


@Root(name = "rss", strict = false)

public class ScienceRSS {

//里面有一个version,如果不加strict = false就会报错

@Element(name = "channel")

public ScienceChannel scienceChannel;

}

第二层


@Root(name = "channel") //根元素

public class ScienceChannel {

@ElementList(inline = true, required = false) //里面是数组

public List mScienceNewsList;

public List getmScienceNewsList() {

return mScienceNewsList;

}

public void setmScienceNewsList(List mScienceNewsList) {

this.mScienceNewsList = mScienceNewsList;

}

}

第三层


@Root(name = "item", strict = false)

public class ScienceNews {

/**

  • 新闻id

*/

@Element(name = "newsid")

public String newsId;

/**

  • 新闻标题

*/

@Element(name = "title")

public String title;

/**

  • 新闻的url(不完整)

*/

@Element(name = "url")

public String url;

/**

  • 更新时间

*/

@Element(name = "postdate")

public String postdate;

/**

  • 图片地址

*/

@Element(name = "image")

public String image;

/**

  • 描述

*/

@Element(name = "description")

public String description;

/**

  • 点击数量

*/

@Element(name = "hitcount")

public String hitCount;

/**

  • 评论数量

*/

@Element(name = "commentcount")

public String commentCount;

/**

  • 禁止评论? false true

*/

@Element(name = "forbidcomment")

public boolean forbidComment;

@Element(name = "cid")

public String cid;

}

正式开始用Simple XML进行解析

===================