android xml网页解析,android上对xml的解析

android下对xml的解析

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 elements

currentstate = 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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值