gpt4 book ai didi

java - Android SAX XML 解析器访问附件标记 URL 属性

转载 作者:行者123 更新时间:2023-11-30 09:44:19 25 4
gpt4 key购买 nike

我已经创建了一个包含详细 View 的 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;
}
}

最佳答案

url 标记是一个属性,因此当 localName 为“enclosure”时,您可以在 startElement 中调用 attributes.getValue("url"); 来访问它。

关于java - Android SAX XML 解析器访问附件标记 URL 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7958017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com