android XML解析

来源:互联网 发布:手机pdf修改软件 编辑:程序博客网 时间:2024/04/30 03:47

 欲解析的XML文档:

[xhtml] view plaincopyprint?
  1.   <?xml version="1.0" ?>   
  2. <rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">  
  3. <channel>  
  4.   <title>CSDN社区 → 社区首页 → 版主推荐技术热贴</title>   
  5.   <link>http://community.csdn.net/</link>   
  6.   <description>CSDN社区 → 社区首页 → 版主推荐技术热贴</description>   
  7.   <language>zh-CN</language>   
  8.   <generator>community.csdn.net</generator>   
  9.   <copyright>Copyright 1998 - 2010 CSDN.NET Inc. All Rights Reserved</copyright>   
  10. <item>  
  11.   <title>服务托盘图标</title>   
  12.   <link>http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html</link>   
  13.   <author>f_tomorrow</author>   
  14.   <guid>http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html</guid>   
  15.   <pubDate>Tue, 21 Dec 2010 06:23:00 GMT</pubDate>   
  16.   <comments>http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html</comments>   
  17.   <slash:comments>0</slash:comments>   
  18.   <source url="http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html">服务托盘图标</source>   
  19.   <description>服务托盘图标</description>   
  20.   </item>  
  21. ......  
  22.   </channel>  
  23.   </rss>  

 

AndroidSaxFeedParser.java

 

[java] view plaincopyprint?
  1. package com.cheerchip.android.xmlcore;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.List;  
  6.   
  7. import org.xml.sax.SAXException;  
  8.   
  9. import android.util.Xml;  
  10.   
  11. import com.cheerchip.android.Topic;  
  12.   
  13. /** 
  14.  * @author jerry.lingchao.wensefu<br/> 
  15.  *         date: 2010-12-21<br/> 
  16.  */  
  17. public class AndroidSaxFeedParser extends BaseFeedParser {  
  18.   
  19.     /** 
  20.      * @param feedUrl 
  21.      */  
  22.     public AndroidSaxFeedParser() {  
  23.     }  
  24.   
  25.     public List<Topic> parse() {  
  26.         RssHandler handler = new RssHandler();  
  27.         try {  
  28.             Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, handler);  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.         } catch (SAXException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         return handler.getTopics();  
  35.     }  
  36.   
  37.     @Override  
  38.     public InputStream getInputStream() {  
  39.         return this.getClass().getClassLoader().getResourceAsStream("csdn.xml");  
  40.     }  
  41.   
  42. }  

 

AndroidSaxFeedParser2.java

 

[java] view plaincopyprint?
  1. package com.cheerchip.android.xmlcore;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.xml.sax.SAXException;  
  9.   
  10. import android.sax.Element;  
  11. import android.sax.EndElementListener;  
  12. import android.sax.EndTextElementListener;  
  13. import android.sax.RootElement;  
  14. import android.util.Xml;  
  15.   
  16. import com.cheerchip.android.Topic;  
  17.   
  18. /** 
  19.  * @author jerry.lingchao.wensefu<br/> 
  20.  *         date: 2010-12-21<br/> 
  21.  */  
  22. public class AndroidSaxFeedParser2 extends BaseFeedParser {  
  23.   
  24.     /** 
  25.      * @param feedUrl 
  26.      */  
  27.     public AndroidSaxFeedParser2() {  
  28.     }  
  29.   
  30.     public List<Topic> parse() {  
  31.         final Topic topic = new Topic();  
  32.         final List<Topic> topics = new ArrayList<Topic>();  
  33.         RootElement root = new RootElement("rss");  
  34.         Element channel = root.getChild("channel");  
  35.         Element item = channel.getChild("item");  
  36.   
  37.         item.setEndElementListener(new EndElementListener() {  
  38.             @Override  
  39.             public void end() {  
  40.                 topics.add(topic.copy());  
  41.             }  
  42.         });  
  43.         item.getChild("title").setEndTextElementListener(  
  44.                 new EndTextElementListener() {  
  45.   
  46.                     @Override  
  47.                     public void end(String body) {  
  48.                         topic.setTitle(body);  
  49.                     }  
  50.                 });  
  51.         item.getChild("link").setEndTextElementListener(  
  52.                 new EndTextElementListener() {  
  53.   
  54.                     @Override  
  55.                     public void end(String body) {  
  56.                         topic.setLink(body);  
  57.                     }  
  58.                 });  
  59.         item.getChild("author").setEndTextElementListener(  
  60.                 new EndTextElementListener() {  
  61.   
  62.                     @Override  
  63.                     public void end(String body) {  
  64.                         topic.setAuthor(body);  
  65.                     }  
  66.                 });  
  67.         item.getChild("pubDate").setEndTextElementListener(  
  68.                 new EndTextElementListener() {  
  69.   
  70.                     @Override  
  71.                     public void end(String body) {  
  72.                         topic.setPubDate(body);  
  73.                     }  
  74.                 });  
  75.         try {  
  76.             Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,  
  77.                     root.getContentHandler());  
  78.         } catch (IOException e) {  
  79.             e.printStackTrace();  
  80.         } catch (SAXException e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.         return topics;  
  84.     }  
  85.   
  86.     @Override  
  87.     public InputStream getInputStream() {  
  88.         return this.getClass().getClassLoader().getResourceAsStream("csdn.xml");  
  89.     }  
  90. }  

 

 

[java] view plaincopyprint?
  1. package com.cheerchip.android.xmlcore;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.xml.sax.Attributes;  
  7. import org.xml.sax.SAXException;  
  8. import org.xml.sax.helpers.DefaultHandler;  
  9.   
  10. import com.cheerchip.android.Topic;  
  11.   
  12. /** 
  13.  * @author jerry.lingchao.wensefu<br/> 
  14.  *         date: 2010-12-21<br/> 
  15.  */  
  16. public class RssHandler extends DefaultHandler {  
  17.   
  18.     private List<Topic> topics;  
  19.     private Topic topic;  
  20.     private StringBuilder sb;  
  21.   
  22.     /** 
  23.      * @return the topics 
  24.      */  
  25.     public List<Topic> getTopics() {  
  26.         return topics;  
  27.     }  
  28.   
  29.     @Override  
  30.     public void characters(char[] ch, int start, int length)  
  31.             throws SAXException {  
  32.         super.characters(ch, start, length);  
  33.         sb.append(ch, start, length);  
  34.     }  
  35.   
  36.     @Override  
  37.     public void startDocument() throws SAXException {  
  38.         super.startDocument();  
  39.         topics = new ArrayList<Topic>();  
  40.         sb = new StringBuilder();  
  41.     }  
  42.   
  43.     @Override  
  44.     public void startElement(String uri, String localName, String qName,  
  45.             Attributes attributes) throws SAXException {  
  46.         super.startElement(uri, localName, qName, attributes);  
  47.         if (localName.equalsIgnoreCase(BaseFeedParser.ITEM)) {  
  48.             this.topic = new Topic();  
  49.         }  
  50.     }  
  51.   
  52.     @Override  
  53.     public void endElement(String uri, String localName, String qName)  
  54.             throws SAXException {  
  55.         super.endElement(uri, localName, qName);  
  56.         if (topic != null) {  
  57.             if (localName.equalsIgnoreCase(BaseFeedParser.TITLE)) {  
  58.                 topic.setTitle(sb.toString());  
  59.             } else if (localName.equalsIgnoreCase(BaseFeedParser.AUTHOR)) {  
  60.                 topic.setAuthor(sb.toString());  
  61.             } else if (localName.equalsIgnoreCase(BaseFeedParser.LINK)) {  
  62.                 topic.setLink(sb.toString());  
  63.             } else if (localName.equalsIgnoreCase(BaseFeedParser.PUB_DATE)) {  
  64.                 topic.setPubDate(sb.toString());  
  65.             } else if (localName.equalsIgnoreCase(BaseFeedParser.ITEM)) {  
  66.                 topics.add(topic);  
  67.             }  
  68.             sb.setLength(0);  
  69.         }  
  70.     }  
  71.   
  72. }  

 

 

实体类:

 

[java] view plaincopyprint?
  1. package com.cheerchip.android;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5. import java.text.ParseException;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Date;  
  8.   
  9. /** 
  10.  * @author jerry.lingchao.wensefu date: 2010-12-21 
  11.  */  
  12. public class Topic {  
  13.   
  14.     private String title;  
  15.   
  16.     private URL link;  
  17.   
  18.     private String author;  
  19.   
  20.     private Date pubDate;  
  21.   
  22.     private static SimpleDateFormat format = new SimpleDateFormat(  
  23.             "EEE, dd MMM yyyy HH:mm:ss Z");  
  24.   
  25.     /** 
  26.      * @return the title 
  27.      */  
  28.     public String getTitle() {  
  29.         return title;  
  30.     }  
  31.   
  32.     /** 
  33.      * @param title 
  34.      *            the title to set 
  35.      */  
  36.     public void setTitle(String title) {  
  37.         this.title = title;  
  38.     }  
  39.   
  40.     /** 
  41.      * @return the link 
  42.      */  
  43.     public URL getLink() {  
  44.         return link;  
  45.     }  
  46.   
  47.     /** 
  48.      * @param link 
  49.      *            the link to set 
  50.      */  
  51.     public void setLink(String link) {  
  52.         try {  
  53.             this.link = new URL(link);  
  54.         } catch (MalformedURLException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
  58.   
  59.     /** 
  60.      * @return the author 
  61.      */  
  62.     public String getAuthor() {  
  63.         return author;  
  64.     }  
  65.   
  66.     /** 
  67.      * @param author 
  68.      *            the author to set 
  69.      */  
  70.     public void setAuthor(String author) {  
  71.         this.author = author;  
  72.     }  
  73.   
  74.     /** 
  75.      * @return the pubDate 
  76.      */  
  77.     public Date getPubDate() {  
  78.         return pubDate;  
  79.     }  
  80.   
  81.     /** 
  82.      * @param pubDate 
  83.      *            the pubDate to set 
  84.      */  
  85.     public void setPubDate(String pubDate) {  
  86.         while (!pubDate.endsWith("00")) {  
  87.             pubDate += "0";  
  88.         }  
  89.         try {  
  90.             this.pubDate = format.parse(pubDate.trim());  
  91.         } catch (ParseException e) {  
  92.             e.printStackTrace();  
  93.         }  
  94.     }  
  95.   
  96.     public Topic copy() {  
  97.         Topic copy = new Topic();  
  98.         copy.author = author;  
  99.         copy.link = link;  
  100.         copy.pubDate = pubDate;  
  101.         copy.title = title;  
  102.         return copy;  
  103.     }  
  104.   
  105.     @Override  
  106.     public String toString() {  
  107.         return "Topic:[title=" + title + ",link=" + link + ",author=" + author  
  108.                 + ",pubDate=" + pubDate + "]";  
  109.     }  
  110.   
  111. }  

 

Activiry

 

[java] view plaincopyprint?
  1. package com.cheerchip.android;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. import com.cheerchip.android.xmlcore.AndroidSaxFeedParser;  
  10. import com.cheerchip.android.xmlcore.AndroidSaxFeedParser2;  
  11.   
  12. public class MainAct extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         setTitle("android XML解析");  
  19.         AndroidSaxFeedParser asf = new AndroidSaxFeedParser();  
  20.         AndroidSaxFeedParser2 asf2 = new AndroidSaxFeedParser2();  
  21.   
  22.         List<Topic> list = asf.parse();  
  23.         List<Topic> list2 = asf2.parse();  
  24.   
  25.         StringBuilder sb = new StringBuilder();  
  26.   
  27. //      for (Topic t : list)  
  28. //          sb.append(t).append("/n");  
  29. //      sb.append(  
  30. //              "---------------------------------------------------------------------------------------------------------------")  
  31. //              .append("/n");  
  32.         for (Topic t : list2)  
  33.             sb.append(t).append("/n");  
  34.         TextView tv = (TextView) findViewById(R.id.text);  
  35.         tv.setText(sb.toString());  
  36.     }  
  37. }  

 

 

效果图:

http://blog.csdn.net/wensefu/article/details/6090112


原创粉丝点击