android Pull解析复杂XML 转

解析代码

Java代码  收藏代码
  1. /** 
  2.      * 从网络地址URL读取XML文件用Pull解析XML---解析jinghua2.xml 
  3.      * @param xmlUrlPath xml的网络地址 
  4.      * @return 阿福 
  5.      * @throws Exception 
  6.      */  
  7.     public static List<News> URLReadXmlByPull(String xmlUrlPath) throws Exception{  
  8.         List<News> listNews =  new ArrayList<News>();;  
  9.         News news = null;  
  10.         URL url = new URL(xmlUrlPath);  
  11.         //构建XmlPullParserFactory  
  12.         XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();  
  13.         //获取XmlPullParser的实例  
  14.         XmlPullParser xmlPullParser = pullParserFactory.newPullParser();  
  15.         Log.i("PullParseXML", "getXML......");  
  16.         //设置输入流  xml文件装载器  
  17.         xmlPullParser.setInput(url.openConnection().getInputStream(), "UTF-8");  
  18.         //开始  
  19.         Log.i("PullParseXML", "PullParseXML....start....");  
  20.         /** 
  21.          * pull读到xml后 返回数字 
  22.          *   读取到xml的声明返回数字0 START_DOCUMENT; 
  23.                读取到xml的结束返回数字1 END_DOCUMENT ; 
  24.                读取到xml的开始标签返回数字2 START_TAG 
  25.                读取到xml的结束标签返回数字3 END_TAG 
  26.                读取到xml的文本返回数字4 TEXT 
  27.          */  
  28.         int eventType=xmlPullParser.getEventType();  
  29.         /** 
  30.          * 只要这个事件返回的不是1 我们就一直读取xml文件 
  31.          */  
  32.         while(eventType != XmlPullParser.END_DOCUMENT){  
  33.             String nodeName=xmlPullParser.getName();  
  34.             switch (eventType) {  
  35.             case XmlPullParser.START_DOCUMENT:  
  36.                 break;  
  37.             case XmlPullParser.START_TAG:  
  38.                 if("item".equals(nodeName)){  
  39.                     news = new News();  
  40.                 }   
  41.                 if("title".equals(nodeName) && news != null){  
  42.                     news.setTitle(xmlPullParser.nextText());  
  43.                 }   
  44.                 if("link".equals(nodeName)  && news != null){  
  45.                     news.setLink(xmlPullParser.nextText());  
  46.                 }  
  47.                 if("author".equals(nodeName)  && news != null){  
  48.                     news.setAuthor(xmlPullParser.nextText());  
  49.                 }  
  50.                 if("guid".equals(nodeName)  && news != null){  
  51.                     news.setGuid(xmlPullParser.nextText());  
  52.                 }  
  53.                 if("image".equals(nodeName)  && news != null){  
  54.                     news.setImage(xmlPullParser.nextText());  
  55.                 }  
  56.                 if("video".equals(nodeName)  && news != null){  
  57.                     news.setVideo(xmlPullParser.nextText());  
  58.                 }  
  59.                 if("category".equals(nodeName)  && news != null){  
  60.                     news.setCategory(xmlPullParser.nextText());  
  61.                 }  
  62.                 if("pubDate".equals(nodeName)  && news != null){  
  63.                     news.setPubDate(xmlPullParser.nextText());  
  64.                 }  
  65.                 if("comments".equals(nodeName)  && news != null){  
  66.                     news.setComments(xmlPullParser.nextText());  
  67.                 }  
  68.                 if("description".equals(nodeName)  && news != null){  
  69.                     news.setDescription(xmlPullParser.nextText());  
  70.                 }  
  71.                   
  72.                 break;  
  73.             case XmlPullParser.END_TAG:  
  74.                 if("item".equals(nodeName)){  
  75.                     listNews.add(news);  
  76.                 }  
  77.                 break;  
  78.   
  79.             default:  
  80.                 break;  
  81.             }  
  82.             eventType = xmlPullParser.next();  
  83.         }  
  84.         return listNews;  
  85.     }  
  86.       

 使用时

Java代码  收藏代码
  1. //从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)  
  2.     public void testPullRead3() throws Throwable{  
  3.         List<News> persons = ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");  
  4.         for(News person : persons){  
  5.             Log.i(TAG, person.toString());  
  6.         }  
  7.     }  

 实体

Java代码  收藏代码
  1. package com.yangguangfu.xml.domain;  
  2.   
  3. /** 
  4.  * 新闻 
  5.  *  
  6.  * @author 阿福 
  7.  *  
  8.  */  
  9. public class News {  
  10.     private String title;  
  11.     private String link;  
  12.     private String author;  
  13.     private String guid;  
  14.     private String image;  
  15.     private String video;  
  16.     private String category;  
  17.     private String pubDate;  
  18.     private String comments;  
  19.     private String description;  
  20.   
  21.     public String getTitle() {  
  22.         return title;  
  23.     }  
  24.   
  25.     public void setTitle(String title) {  
  26.         this.title = title;  
  27.     }  
  28.   
  29.     public String getLink() {  
  30.         return link;  
  31.     }  
  32.   
  33.     public void setLink(String link) {  
  34.         this.link = link;  
  35.     }  
  36.   
  37.     public String getAuthor() {  
  38.         return author;  
  39.     }  
  40.   
  41.     public void setAuthor(String author) {  
  42.         this.author = author;  
  43.     }  
  44.   
  45.     public String getGuid() {  
  46.         return guid;  
  47.     }  
  48.   
  49.     public void setGuid(String guid) {  
  50.         this.guid = guid;  
  51.     }  
  52.   
  53.     public String getImage() {  
  54.         return image;  
  55.     }  
  56.   
  57.     public void setImage(String image) {  
  58.         this.image = image;  
  59.     }  
  60.   
  61.     public String getVideo() {  
  62.         return video;  
  63.     }  
  64.   
  65.     public void setVideo(String video) {  
  66.         this.video = video;  
  67.     }  
  68.   
  69.     public String getCategory() {  
  70.         return category;  
  71.     }  
  72.   
  73.     public void setCategory(String category) {  
  74.         this.category = category;  
  75.     }  
  76.   
  77.     public String getPubDate() {  
  78.         return pubDate;  
  79.     }  
  80.   
  81.     public void setPubDate(String pubDate) {  
  82.         this.pubDate = pubDate;  
  83.     }  
  84.   
  85.     public String getComments() {  
  86.         return comments;  
  87.     }  
  88.   
  89.     public void setComments(String comments) {  
  90.         this.comments = comments;  
  91.     }  
  92.   
  93.     public String getDescription() {  
  94.         return description;  
  95.     }  
  96.   
  97.     public void setDescription(String description) {  
  98.         this.description = description;  
  99.     }  
  100.   
  101.     @Override  
  102.     public String toString() {  
  103.         return "title=" + title + "guid=" + guid + ",link=" + link  
  104.                 + ",description=" + description + ",image=" + image + ",video="  
  105.                 + video + ",category=" + category + ",author=" + author  
  106.                 + ",pubDate=" + pubDate + ",comments=" + comments;  
  107.     }  
  108.   
  109. }  

posted on 2012-06-17 09:41  民谣  阅读(1139)  评论(2编辑  收藏  举报

导航