android xml pull,android Pull解析复杂XML(转)

解析代码

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

publicstaticList URLReadXmlByPull(String xmlUrlPath)throwsException{

List listNews = newArrayList();;

News news = null;

URL url = newURL(xmlUrlPath);

//构建XmlPullParserFactory

XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();

//获取XmlPullParser的实例

XmlPullParser xmlPullParser = pullParserFactory.newPullParser();

Log.i("PullParseXML","getXML......");

//设置输入流 xml文件装载器

xmlPullParser.setInput(url.openConnection().getInputStream(), "UTF-8");

//开始

Log.i("PullParseXML","PullParseXML....start....");

inteventType=xmlPullParser.getEventType();

while(eventType != XmlPullParser.END_DOCUMENT){

String nodeName=xmlPullParser.getName();

switch(eventType) {

caseXmlPullParser.START_DOCUMENT:

break;

caseXmlPullParser.START_TAG:

if("item".equals(nodeName)){

news = newNews();

}

if("title".equals(nodeName) && news !=null){

news.setTitle(xmlPullParser.nextText());

}

if("link".equals(nodeName) && news !=null){

news.setLink(xmlPullParser.nextText());

}

if("author".equals(nodeName) && news !=null){

news.setAuthor(xmlPullParser.nextText());

}

if("guid".equals(nodeName) && news !=null){

news.setGuid(xmlPullParser.nextText());

}

if("image".equals(nodeName) && news !=null){

news.setImage(xmlPullParser.nextText());

}

if("video".equals(nodeName) && news !=null){

news.setVideo(xmlPullParser.nextText());

}

if("category".equals(nodeName) && news !=null){

news.setCategory(xmlPullParser.nextText());

}

if("pubDate".equals(nodeName) && news !=null){

news.setPubDate(xmlPullParser.nextText());

}

if("comments".equals(nodeName) && news !=null){

news.setComments(xmlPullParser.nextText());

}

if("description".equals(nodeName) && news !=null){

news.setDescription(xmlPullParser.nextText());

}

break;

caseXmlPullParser.END_TAG:

if("item".equals(nodeName)){

listNews.add(news);

}

break;

default:

break;

}

eventType = xmlPullParser.next();

}

returnlistNews;

}

public static List URLReadXmlByPull(String xmlUrlPath) throws Exception{

List listNews = new ArrayList();;

News news = null;

URL url = new URL(xmlUrlPath);

//构建XmlPullParserFactory

XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();

//获取XmlPullParser的实例

XmlPullParser xmlPullParser = pullParserFactory.newPullParser();

Log.i("PullParseXML", "getXML......");

//设置输入流 xml文件装载器

xmlPullParser.setInput(url.openConnection().getInputStream(), "UTF-8");

//开始

Log.i("PullParseXML", "PullParseXML....start....");

int eventType=xmlPullParser.getEventType();

while(eventType != XmlPullParser.END_DOCUMENT){

String nodeName=xmlPullParser.getName();

switch (eventType) {

case XmlPullParser.START_DOCUMENT:

break;

case XmlPullParser.START_TAG:

if("item".equals(nodeName)){

news = new News();

}

if("title".equals(nodeName) && news != null){

news.setTitle(xmlPullParser.nextText());

}

if("link".equals(nodeName) && news != null){

news.setLink(xmlPullParser.nextText());

}

if("author".equals(nodeName) && news != null){

news.setAuthor(xmlPullParser.nextText());

}

if("guid".equals(nodeName) && news != null){

news.setGuid(xmlPullParser.nextText());

}

if("image".equals(nodeName) && news != null){

news.setImage(xmlPullParser.nextText());

}

if("video".equals(nodeName) && news != null){

news.setVideo(xmlPullParser.nextText());

}

if("category".equals(nodeName) && news != null){

news.setCategory(xmlPullParser.nextText());

}

if("pubDate".equals(nodeName) && news != null){

news.setPubDate(xmlPullParser.nextText());

}

if("comments".equals(nodeName) && news != null){

news.setComments(xmlPullParser.nextText());

}

if("description".equals(nodeName) && news != null){

news.setDescription(xmlPullParser.nextText());

}

break;

case XmlPullParser.END_TAG:

if("item".equals(nodeName)){

listNews.add(news);

}

break;

default:

break;

}

eventType = xmlPullParser.next();

}

return listNews;

}

使用时

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

//从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)

publicvoidtestPullRead3()throwsThrowable{

List persons = ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");

for(News person : persons){

Log.i(TAG, person.toString());

}

}

//从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)

public void testPullRead3() throws Throwable{

List persons = ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");

for(News person : persons){

Log.i(TAG, person.toString());

}

}

实体

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

packagecom.yangguangfu.xml.domain;

publicclassNews {

privateString title;

privateString link;

privateString author;

privateString guid;

privateString image;

privateString video;

privateString category;

privateString pubDate;

privateString comments;

privateString description;

publicString getTitle() {

returntitle;

}

publicvoidsetTitle(String title) {

this.title = title;

}

publicString getLink() {

returnlink;

}

publicvoidsetLink(String link) {

this.link = link;

}

publicString getAuthor() {

returnauthor;

}

publicvoidsetAuthor(String author) {

this.author = author;

}

publicString getGuid() {

returnguid;

}

publicvoidsetGuid(String guid) {

this.guid = guid;

}

publicString getImage() {

returnimage;

}

publicvoidsetImage(String image) {

this.image = image;

}

publicString getVideo() {

returnvideo;

}

publicvoidsetVideo(String video) {

this.video = video;

}

publicString getCategory() {

returncategory;

}

publicvoidsetCategory(String category) {

this.category = category;

}

publicString getPubDate() {

returnpubDate;

}

publicvoidsetPubDate(String pubDate) {

this.pubDate = pubDate;

}

publicString getComments() {

returncomments;

}

publicvoidsetComments(String comments) {

this.comments = comments;

}

publicString getDescription() {

returndescription;

}

publicvoidsetDescription(String description) {

this.description = description;

}

@Override

publicString toString() {

return"title="+ title +"guid="+ guid +",link="+ link

+ ",description="+ description +",image="+ image +",video="

+ video + ",category="+ category +",author="+ author

+ ",pubDate="+ pubDate +",comments="+ comments;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值