欲解析的XML文档:
- <?xml version="1.0" ?>
- - <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">
- - <channel>
- <title>CSDN社区 → 社区首页 → 版主推荐技术热贴</title>
- <link>http://community.csdn.net/</link>
- <description>CSDN社区 → 社区首页 → 版主推荐技术热贴</description>
- <language>zh-CN</language>
- <generator>community.csdn.net</generator>
- <copyright>Copyright 1998 - 2010 CSDN.NET Inc. All Rights Reserved</copyright>
- - <item>
- <title>服务托盘图标</title>
- <link>http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html</link>
- <author>f_tomorrow</author>
- <guid>http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html</guid>
- <pubDate>Tue, 21 Dec 2010 06:23:00 GMT</pubDate>
- <comments>http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html</comments>
- <slash:comments>0</slash:comments>
- <source url="http://topic.csdn.net/u/20101221/11/475FDC82-A689-42C4-BBFF-9AF3C75587C1.html">服务托盘图标</source>
- <description>服务托盘图标</description>
- </item>
- ......
- </channel>
- </rss>
AndroidSaxFeedParser.java
- package com.cheerchip.android.xmlcore;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- import org.xml.sax.SAXException;
- import android.util.Xml;
- import com.cheerchip.android.Topic;
- /**
- * @author jerry.lingchao.wensefu<br/>
- * date: 2010-12-21<br/>
- */
- public class AndroidSaxFeedParser extends BaseFeedParser {
- /**
- * @param feedUrl
- */
- public AndroidSaxFeedParser() {
- }
- public List<Topic> parse() {
- RssHandler handler = new RssHandler();
- try {
- Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, handler);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- return handler.getTopics();
- }
- @Override
- public InputStream getInputStream() {
- return this.getClass().getClassLoader().getResourceAsStream("csdn.xml");
- }
- }
AndroidSaxFeedParser2.java
- package com.cheerchip.android.xmlcore;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.SAXException;
- import android.sax.Element;
- import android.sax.EndElementListener;
- import android.sax.EndTextElementListener;
- import android.sax.RootElement;
- import android.util.Xml;
- import com.cheerchip.android.Topic;
- /**
- * @author jerry.lingchao.wensefu<br/>
- * date: 2010-12-21<br/>
- */
- public class AndroidSaxFeedParser2 extends BaseFeedParser {
- /**
- * @param feedUrl
- */
- public AndroidSaxFeedParser2() {
- }
- public List<Topic> parse() {
- final Topic topic = new Topic();
- final List<Topic> topics = new ArrayList<Topic>();
- RootElement root = new RootElement("rss");
- Element channel = root.getChild("channel");
- Element item = channel.getChild("item");
- item.setEndElementListener(new EndElementListener() {
- @Override
- public void end() {
- topics.add(topic.copy());
- }
- });
- item.getChild("title").setEndTextElementListener(
- new EndTextElementListener() {
- @Override
- public void end(String body) {
- topic.setTitle(body);
- }
- });
- item.getChild("link").setEndTextElementListener(
- new EndTextElementListener() {
- @Override
- public void end(String body) {
- topic.setLink(body);
- }
- });
- item.getChild("author").setEndTextElementListener(
- new EndTextElementListener() {
- @Override
- public void end(String body) {
- topic.setAuthor(body);
- }
- });
- item.getChild("pubDate").setEndTextElementListener(
- new EndTextElementListener() {
- @Override
- public void end(String body) {
- topic.setPubDate(body);
- }
- });
- try {
- Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
- root.getContentHandler());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- return topics;
- }
- @Override
- public InputStream getInputStream() {
- return this.getClass().getClassLoader().getResourceAsStream("csdn.xml");
- }
- }
- package com.cheerchip.android.xmlcore;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import com.cheerchip.android.Topic;
- /**
- * @author jerry.lingchao.wensefu<br/>
- * date: 2010-12-21<br/>
- */
- public class RssHandler extends DefaultHandler {
- private List<Topic> topics;
- private Topic topic;
- private StringBuilder sb;
- /**
- * @return the topics
- */
- public List<Topic> getTopics() {
- return topics;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- super.characters(ch, start, length);
- sb.append(ch, start, length);
- }
- @Override
- public void startDocument() throws SAXException {
- super.startDocument();
- topics = new ArrayList<Topic>();
- sb = new StringBuilder();
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- super.startElement(uri, localName, qName, attributes);
- if (localName.equalsIgnoreCase(BaseFeedParser.ITEM)) {
- this.topic = new Topic();
- }
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- super.endElement(uri, localName, qName);
- if (topic != null) {
- if (localName.equalsIgnoreCase(BaseFeedParser.TITLE)) {
- topic.setTitle(sb.toString());
- } else if (localName.equalsIgnoreCase(BaseFeedParser.AUTHOR)) {
- topic.setAuthor(sb.toString());
- } else if (localName.equalsIgnoreCase(BaseFeedParser.LINK)) {
- topic.setLink(sb.toString());
- } else if (localName.equalsIgnoreCase(BaseFeedParser.PUB_DATE)) {
- topic.setPubDate(sb.toString());
- } else if (localName.equalsIgnoreCase(BaseFeedParser.ITEM)) {
- topics.add(topic);
- }
- sb.setLength(0);
- }
- }
- }
实体类:
- package com.cheerchip.android;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- * @author jerry.lingchao.wensefu date: 2010-12-21
- */
- public class Topic {
- private String title;
- private URL link;
- private String author;
- private Date pubDate;
- private static SimpleDateFormat format = new SimpleDateFormat(
- "EEE, dd MMM yyyy HH:mm:ss Z");
- /**
- * @return the title
- */
- public String getTitle() {
- return title;
- }
- /**
- * @param title
- * the title to set
- */
- public void setTitle(String title) {
- this.title = title;
- }
- /**
- * @return the link
- */
- public URL getLink() {
- return link;
- }
- /**
- * @param link
- * the link to set
- */
- public void setLink(String link) {
- try {
- this.link = new URL(link);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- /**
- * @return the author
- */
- public String getAuthor() {
- return author;
- }
- /**
- * @param author
- * the author to set
- */
- public void setAuthor(String author) {
- this.author = author;
- }
- /**
- * @return the pubDate
- */
- public Date getPubDate() {
- return pubDate;
- }
- /**
- * @param pubDate
- * the pubDate to set
- */
- public void setPubDate(String pubDate) {
- while (!pubDate.endsWith("00")) {
- pubDate += "0";
- }
- try {
- this.pubDate = format.parse(pubDate.trim());
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- public Topic copy() {
- Topic copy = new Topic();
- copy.author = author;
- copy.link = link;
- copy.pubDate = pubDate;
- copy.title = title;
- return copy;
- }
- @Override
- public String toString() {
- return "Topic:[title=" + title + ",link=" + link + ",author=" + author
- + ",pubDate=" + pubDate + "]";
- }
- }
Activiry
- package com.cheerchip.android;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- import com.cheerchip.android.xmlcore.AndroidSaxFeedParser;
- import com.cheerchip.android.xmlcore.AndroidSaxFeedParser2;
- public class MainAct extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setTitle("android XML解析");
- AndroidSaxFeedParser asf = new AndroidSaxFeedParser();
- AndroidSaxFeedParser2 asf2 = new AndroidSaxFeedParser2();
- List<Topic> list = asf.parse();
- List<Topic> list2 = asf2.parse();
- StringBuilder sb = new StringBuilder();
- // for (Topic t : list)
- // sb.append(t).append("/n");
- // sb.append(
- // "---------------------------------------------------------------------------------------------------------------")
- // .append("/n");
- for (Topic t : list2)
- sb.append(t).append("/n");
- TextView tv = (TextView) findViewById(R.id.text);
- tv.setText(sb.toString());
- }
- }
效果图:
http://blog.csdn.net/wensefu/article/details/6090112