android 解析xml
1.使用SAXParser
    try    {           URL url = new URL(urlToRssFeed);           // create the factory           SAXParserFactory factory = SAXParserFactory.newInstance();           // create a parser           SAXParser parser = factory.newSAXParser();           // create the reader (scanner)           XMLReader xmlreader = parser.getXMLReader();           // instantiate our handler           RSSHandler theRssHandler = new RSSHandler();           // assign our handler           xmlreader.setContentHandler(theRssHandler);           // get our data via the url class           InputSource is = new InputSource(url.openStream());           // perform the synchronous parse                      xmlreader.parse(is);           // get the results - should be a fully populated RSSFeed instance, or null on error           return theRssHandler.getFeed();    }    catch (Exception ee)    {    // if we have a problem, simply return null    return null;    }


handle片段
实现startDocument,endDocument,startElement,endElement四个方法
开始文档初始化数组,开始元素时初始化元素,完成元素时,把内容添加元素对象中,再把元素对象添加到对象数组中
最后返回对象数组

RSSFeed getFeed(){return _feed;}public void startDocument() throws SAXException{// initialize our RSSFeed object - this will hold our parsed contents_feed = new RSSFeed();// initialize the RSSItem object - we will use this as a crutch to grab the info from the channel// because the channel and items have very similar entries.._item = new RSSItem();}public void endDocument() throws SAXException{}public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException{depth++;if (localName.equals("channel")){currentstate = 0;return;}if (localName.equals("image")){// record our feed data - we temporarily stored it in the item :)_feed.setTitle(_item.getTitle());_feed.setPubDate(_item.getPubDate());}if (localName.equals("item")){// create a new item_item = new RSSItem();return;}if (localName.equals("title")){currentstate = RSS_TITLE;return;}if (localName.equals("description")){currentstate = RSS_DESCRIPTION;return;}if (localName.equals("link")){currentstate = RSS_LINK;return;}if (localName.equals("category")){currentstate = RSS_CATEGORY;return;}if (localName.equals("pubDate")){currentstate = RSS_PUBDATE;return;}// if we don't explicitly handle the element, make sure we don't wind up erroneously // storing a newline or other bogus data into one of our existing elementscurrentstate = 0;}public void endElement(String namespaceURI, String localName, String qName) throws SAXException{depth--;if (localName.equals("item")){// add our item to the list!_feed.addItem(_item);return;}}



2.使用XmlPullParser来解析rss

       // TODO: switch to sax        XmlPullParser xpp = Xml.newPullParser();        xpp.setInput(in, null);  // null = default to UTF-8        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();        }

更多相关文章

  1. Android EditText 共用TextWatcher,在TextWatcher中确定对应的被
  2. Android 时间对象操作工具类
  3. Android中的Message类以及Java对象池的实现
  4. Android最简单的使用数组的适配器Adapter
  5. android 中通过 aidl 传递map对象
  6. Android 各种布局技术-五大布局对象
  7. 谁说Android的动画不廉价(三)之共享元素动画

随机推荐

  1. Android versionCode与versionName
  2. 基于android平台底部菜单实现
  3. Android开发中ANR详解及解决办法
  4. Android ashmem的实现方式
  5. Android导入第三方jar包,proguard混淆脚本
  6. android 同时发送几条通知
  7. 自定义RatingBar的样式
  8. 记一次Android(安卓)OOM探险之旅
  9. 从网络获取图片,并缓存到SD卡
  10. Picasso学习