我创建了一个包含详细信息视图的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("RSSReader","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;
}
}
答案 0 :(得分:2)
url标记是一个属性,因此要访问它,当attributes.getValue("url");
为“enclosure”时,您可以在startElement中调用localName
。