Android SAX XML Parser Access Enclosure 标签 URL 属性
Posted
技术标签:
【中文标题】Android SAX XML Parser Access Enclosure 标签 URL 属性【英文标题】:Android SAX XML Parser Access Enclosure Tag URL Attribute 【发布时间】:2011-12-18 23:18:15 【问题描述】:我创建了一个包含详细视图的 XML 解析器。我正在阅读包含附件标签的 RSS 提要。我正在尝试从附件标签中获取视频文件的 URL。
这是附件标签的样子:
<enclosure url="http://video.calvaryccm.com/main/podcastmp4/MB66-03.mp4" type="video/mp4"/>
每次我尝试检索它时,解析器都会返回 null。我该怎么办?
这是显示似乎导致问题的解析器结果的部分:
// TESTING HERE!
String enclosure = b.getString("enclosure");
// What is getting displayed
theTitle = b.getString("title").trim();
theDate = newDateStr;
theStory = b.getString("description") + "\n\nView in full website:\n" + b.getString("link")+ "\n\nDownload teaching:\n" + enclosure;
这是 RSSHandler 类:
public class RSSHandler extends DefaultHandler
RSSFeed _feed;
RSSItem _item;
String _lastElementName = "";
boolean bFoundChannel = false;
final int RSS_TITLE = 1;
final int RSS_LINK = 2;
final int RSS_DESCRIPTION = 3;
final int RSS_ENCLOSURE = 4;
final int RSS_PUBDATE = 5;
int depth = 0;
int currentstate = 0;
/*
* Constructor
*/
RSSHandler()
/*
* getFeed - this returns the feed when all of the parsing is complete
*/
RSSFeed getFeed()
return _feed;
public void startDocument() throws SAXException
// Initialize RSSFeed object - this will hold 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 feed data - 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("enclosure"))
currentstate = RSS_ENCLOSURE;
return;
if (localName.equals("pubDate"))
currentstate = RSS_PUBDATE;
return;
// If I don't explicitly handle the element and to make sure I don't wind up erroneously
// storing a newline or other fake 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 the item to the list!
_feed.addItem(_item);
return;
public void characters(char ch[], int start, int length)
String theString = new String(ch,start,length);
Log.i("Rs-s-reader","characters[" + theString + "]");
switch (currentstate)
case RSS_TITLE:
_item.setTitle(theString);
currentstate = 0;
break;
case RSS_LINK:
_item.setLink(theString);
currentstate = 0;
break;
case RSS_DESCRIPTION:
_item.setDescription(theString);
currentstate = 0;
break;
case RSS_ENCLOSURE:
_item.setEnclosure(theString);
currentstate = 0;
break;
case RSS_PUBDATE:
_item.setPubDate(theString);
currentstate = 0;
break;
default:
return;
这是 RSSFeed 类:
public class RSSFeed
private String _title = null;
private String _pubdate = null;
private String _enclosure = null;
private int _itemcount = 0;
private List<RSSItem> _itemlist;
RSSFeed()
_itemlist = new Vector<RSSItem>(0);
int addItem(RSSItem item)
_itemlist.add(item);
_itemcount++;
return _itemcount;
RSSItem getItem(int location)
return _itemlist.get(location);
List<RSSItem> getAllItems()
return _itemlist;
int getItemCount()
return _itemcount;
void setTitle(String title)
_title = title;
void setPubDate(String pubdate)
_pubdate = pubdate;
void setEnclosure(String enclosure)
_enclosure = enclosure;
String getTitle()
return _title;
String getPubDate()
return _pubdate;
String getEnclosure()
return _enclosure;
这是 RSSItem 类:
public class RSSItem
private String _title = null;
private String _description = null;
private String _link = null;
private String _enclosure = null;
private String _pubdate = null;
RSSItem()
void setTitle(String title)
_title = title;
void setDescription(String description)
_description = description;
void setLink(String link)
_link = link;
void setEnclosure(String enclosure)
_enclosure = enclosure;
void setPubDate(String pubdate)
_pubdate = pubdate;
String getTitle()
return _title;
String getDescription()
return _description;
String getLink()
return _link;
String getEnclosure()
return _enclosure;
String getPubDate()
return _pubdate;
public String toString()
// limit how much text we display
if (_title.length() > 42)
return _title.substring(0, 42) + "...";
return _title;
【问题讨论】:
你的b
是什么对象?您的所有对象都没有定义 getString(String)
方法。
b 是我用来将数据从一个类传输到另一个类的包的名称 像这样:Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");跨度>
【参考方案1】:
url标签是一个属性,所以当localName
是“enclosure”时,你可以在startElement中调用attributes.getValue("url");
来访问它。
【讨论】:
以上是关于Android SAX XML Parser Access Enclosure 标签 URL 属性的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android 中使用 SAX Parser 为解析的 xml 文件设置 UTF-8
XML Pull Parser 和 SAX Parser 有啥区别
使用 Sax Parser、Java 处理 XML 中的空标签