Chinaunix首页 | 论坛 | 博客
  • 博客访问: 822218
  • 博文数量: 210
  • 博客积分: 10002
  • 博客等级: 上将
  • 技术积分: 1840
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 09:56
文章分类

全部博文(210)

文章存档

2011年(1)

2010年(6)

2009年(65)

2008年(138)

我的朋友

分类: LINUX

2008-12-04 10:38:06

最好的方法是学习RssReader的代码,在android开放出来的代码中,包含了这个范例代码。

其支持SAX和XPP两种方式。

 
    /**
     * Does rudimentary RSS parsing on the given stream and posts rss items to
     * the UI as they are found. Uses Android's XmlPullParser facility. This is
     * not a production quality RSS parser -- it just does a basic job of it.
     *
     * @param in stream to read
     * @param adapter adapter for ui events
     */
    void parseRSS(InputStream in, RSSListAdapter adapter) throws IOException,
            XmlPullParserException {
        // TODO: switch to sax

        XmlPullParser xpp = Xml.newPullParser();
        xpp.setInput(in, null);  // null = parser figures out encoding

        int eventType;
        String title = "";
        String link = "";
        String description = "";
        eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                String tag = xpp.getName();
                if (tag.equals("item")) {
                    title = link = description = "";
                } else if (tag.equals("title")) {
                    xpp.next(); // Skip to next element -- assume text is directly inside the tag
                    title = xpp.getText();
                } else if (tag.equals("link")) {
                    xpp.next();
                    link = xpp.getText();
                } else if (tag.equals("description")) {
                    xpp.next();
                    description = xpp.getText();
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                // We have a comlete item -- post it back to the UI
                // using the mHandler (necessary because we are not
                // running on the UI thread).
                String tag = xpp.getName();
                if (tag.equals("item")) {
                    RssItem item = new RssItem(title, link, description);
                    mHandler.post(new ItemAdder(item));
                }
            }
            eventType = xpp.next();
        }
    }
   
    // SAX version of the code to do the parsing.
    /*
    private class RSSHandler extends DefaultHandler {
        RSSListAdapter mAdapter;
       
        String mTitle;
        String mLink;
        String mDescription;
       
        StringBuilder mBuff;
       
        boolean mInItem;
       
        public RSSHandler(RSSListAdapter adapter) {
            mAdapter = adapter;
            mInItem = false;
            mBuff = new StringBuilder();
        }
       
        public void startElement(String uri,
                String localName,
                String qName,
                Attributes atts)
                throws SAXException {
            String tag = localName;
            if (tag.equals("")) tag = qName;
           
            // If inside , clear out buff on each tag start
            if (mInItem) {
                mBuff.delete(0, mBuff.length());
            }
           
            if (tag.equals("item")) {
                mTitle = mLink = mDescription = "";
                mInItem = true;
            }
        }
       
        public void characters(char[] ch,
                      int start,
                      int length)
                      throws SAXException {
            // Buffer up all the chars when inside
            if (mInItem) mBuff.append(ch, start, length);
        }
                     
        public void endElement(String uri,
                      String localName,
                      String qName)
                      throws SAXException {
            String tag = localName;
            if (tag.equals("")) tag = qName;
           
            // For each tag, copy buff chars to right variable
            if (tag.equals("title")) mTitle = mBuff.toString();
            else if (tag.equals("link")) mLink = mBuff.toString();
            if (tag.equals("description")) mDescription = mBuff.toString();
           
            // Have all the data at this point .... post it to the UI.
            if (tag.equals("item")) {
                RssItem item = new RssItem(mTitle, mLink, mDescription);
                mHandler.post(new ItemAdder(item));
                mInItem = false;
            }
        }
    }
    */
   
    /*
    public void parseRSS2(InputStream in, RSSListAdapter adapter) throws IOException {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            DefaultHandler handler = new RSSHandler(adapter);
           
            parser.parse(in, handler);
            // TODO: does the parser figure out the encoding right on its own?
    }
    */
}

阅读(1789) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

kjsolo2012-04-11 20:51:02

请问RssReader的源代码在哪里有?