android下对xml的解析
16lz
2021-01-23
android 解析xml
1.使用SAXParser
handle片段
实现startDocument,endDocument,startElement,endElement四个方法
开始文档初始化数组,开始元素时初始化元素,完成元素时,把内容添加元素对象中,再把元素对象添加到对象数组中
最后返回对象数组
2.使用XmlPullParser来解析rss
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(); }
更多相关文章
- Android EditText 共用TextWatcher,在TextWatcher中确定对应的被
- Android 时间对象操作工具类
- Android中的Message类以及Java对象池的实现
- Android最简单的使用数组的适配器Adapter
- android 中通过 aidl 传递map对象
- Android 各种布局技术-五大布局对象
- 谁说Android的动画不廉价(三)之共享元素动画