我在android上解析rss feed xml并且能够检索所有其他内容但是无法检索到Feed中的图像。
我正在使用sax解析器。
图片嵌入<media:thumbnail>
标签中。
看起来像这样。
<media:thumbnail>http://www.*some_website*.com/..../Client000040008200000082068.jpg</media:thumbnail>
我见过很多例子,但是他们从链接嵌入式URL解析,也看到了像这样嵌入的链接
<media:thumbnail="link here"></media:thumbnail>
他们俩都一样吗? 以及如何解析这些标签?
请帮我解答。
谢谢。
修改
这是我的rss处理程序
public void startDocument() throws SAXException {
mFeed = new Feed();
}
public void endDocument() throws SAXException {
Date now = new Date();
//Date now = Calendar.getInstance().getTime();
mFeed.setRefresh(now);
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//Only consider elements from allowed third-party namespaces
if (NAMESPACES.contains(uri)) {
mSb = new StringBuffer();
String value = localName.trim();
if (value.equalsIgnoreCase("rss") || value.equalsIgnoreCase("rdf")) {
isType = true;
} else if (value.equalsIgnoreCase("feed")) {
isType = true;
isFeed = true;
} else if (value.equalsIgnoreCase("channel")) {
isFeed = true;
} else if (value.equalsIgnoreCase("item") || value.equalsIgnoreCase("entry")) {
mItem = new Item();
isItem = true;
mNbrItems++;
} else if (value.equalsIgnoreCase("title"))
isTitle = true;
else if (value.equalsIgnoreCase("link")) {
// Get attributes from link element for Atom format
if (attributes != null) {
// Enclosure for Atom format
if (attributes.getValue("rel") != null && attributes.getValue("rel").equalsIgnoreCase("enclosure")) {
mEnclosure = new Enclosure();
mMimeAttribute = attributes.getValue("type");
isEnclosure = true;
}
mHrefAttribute = attributes.getValue("href");
}
isLink = true;
} else if (value.equalsIgnoreCase("pubDate") || value.equalsIgnoreCase("published") || value.equalsIgnoreCase("date"))
isPubdate = true;
else if (value.equalsIgnoreCase("guid") || value.equalsIgnoreCase("id"))
isGuid = true;
else if (value.equalsIgnoreCase("description") || value.equalsIgnoreCase("summary"))
isDescription = true;
else if ((value.trim().equals("media:thumbnail")))
{
// isImageLink = true;
String attrValue = attributes.getValue("url");
int tmp =Integer.parseInt(attrValue);
// tmp.setLink(attrValue);
/*if (localName.trim().equals("thumbnail")) {
String thumbnail = attributes.getValue("url");
}*/
}
else if (value.equalsIgnoreCase("encoded") || value.equalsIgnoreCase("content"))
isContent = true;
else if (value.equalsIgnoreCase("source"))
isSource = true;
else if (value.equalsIgnoreCase("enclosure")) {
// Enclosure for RSS format
if (attributes != null) {
mEnclosure = new Enclosure();
mMimeAttribute = attributes.getValue("type");
mHrefAttribute = attributes.getValue("url");
isEnclosure = true;
}
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
//Only consider elements from allowed third-party namespaces
if (NAMESPACES.contains(uri)) {
String value = localName.trim();
if (value.equalsIgnoreCase("rss")) {
mFeed.setType(Feed.TYPE_RSS);
isType = false;
} else if (value.equalsIgnoreCase("feed")) {
mFeed.setType(Feed.TYPE_ATOM);
isType = false;
isFeed = false;
} else if (value.equalsIgnoreCase("RDF")) {
mFeed.setType(Feed.TYPE_RDF);
isType = false;
} else if (value.equalsIgnoreCase("channel")) {
isFeed = false;
} else if (value.equalsIgnoreCase("item") || value.equalsIgnoreCase("entry")) {
if (mNbrItems <= maxItems) {
if (mItem.getGuid() == null)
mItem.setGuid(mItem.getLink().toString());
mFeed.addItem(mItem);
}
isItem = false;
} else if (value.equalsIgnoreCase("title") && !isSource) {
if (isItem)
mItem.setTitle(Html.fromHtml(mSb.toString().trim()).toString());
else if (isFeed)
mFeed.setTitle(Html.fromHtml(mSb.toString().trim()).toString());
isTitle = false;
} else if (value.equalsIgnoreCase("link") && !isSource) {
if (isItem) {
try {
if (isEnclosure) {
// Enclosure for Atom format
mEnclosure.setMime(mMimeAttribute);
mEnclosure.setURL(new URL(mHrefAttribute));
mItem.addEnclosure(mEnclosure);
mMimeAttribute = null;
isEnclosure = false;
} else if (mHrefAttribute != null)
mItem.setLink(new URL(mHrefAttribute));
else
mItem.setLink(new URL(mSb.toString().trim()));
} catch(MalformedURLException mue) {
throw new SAXException(mue);
}
} else if (isFeed && mFeed.getHomePage() == null) {
try {
if (mSb != null && mSb.toString() != "") // RSS
mFeed.setHomePage(new URL(mSb.toString().trim()));
else if (mMimeAttribute == "text/html") //Atom
mFeed.setHomePage(new URL(mHrefAttribute));
} catch(MalformedURLException mue) {
throw new SAXException(mue);
}
}
mHrefAttribute = null;
isLink = false;
} else if (value.equalsIgnoreCase("pubDate") || value.equalsIgnoreCase("published") || value.equalsIgnoreCase("date")) {
if (isItem) {
for (int i = 0; i < DATE_FORMATS.length; i++) {
try {
//String pattern = mSimpleDateFormats[i].toPattern();
mItem.setPubdate(mSimpleDateFormats[i].parse(mSb.toString().trim()));
break;
} catch (ParseException pe) {
if (i == DATE_FORMATS.length-1) {
throw new SAXException(pe);
}
}
}
}
isPubdate = false;
} else if ((value.equalsIgnoreCase("guid") || value.equalsIgnoreCase("id")) && !isSource) {
if (isItem)
mItem.setGuid(mSb.toString().trim());
isGuid = false;
} else if (value.equalsIgnoreCase("description") || value.equalsIgnoreCase("summary")) {
if (isItem)
//mItem.setContent(Html.fromHtml(mSb.toString().trim()).toString());
mItem.setContent(removeContentSpanObjects(mSb).toString().trim() + System.getProperty("line.separator" ));
isDescription = false;
} else if (value.equalsIgnoreCase("media:thumbnail") || value.equalsIgnoreCase("media:content")) {
if (isItem)
//mItem.setContent(Html.fromHtml(mSb.toString().trim()).toString());
System.out.println(removeContentSpanObjects(mSb).toString().trim());
mItem.setMimageLink(removeContentSpanObjects(mSb).toString().trim());
isImageLink = false;
} else if (value.equalsIgnoreCase("encoded") || value.equalsIgnoreCase("content")) {
if (isItem)
//mItem.setContent(Html.fromHtml(mSb.toString().trim()).toString());
mItem.setContent(removeContentSpanObjects(mSb).toString().trim() + System.getProperty("line.separator" ));
isContent = false;
} else if (value.equalsIgnoreCase("source"))
isSource = false;
else if (value.equalsIgnoreCase("enclosure")) {
if (isItem) {
try {
// Enclosure for RSS format
mEnclosure.setMime(mMimeAttribute);
mEnclosure.setURL(new URL(mHrefAttribute));
mItem.addEnclosure(mEnclosure);
mMimeAttribute = null;
mHrefAttribute = null;
} catch(MalformedURLException mue) {
throw new SAXException(mue);
}
}
isEnclosure = false;
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (isType || isTitle || isLink || isPubdate || isGuid || isDescription || isImageLink || isContent)
mSb.append(new String(ch, start, length));
}
public Feed handleFeed(URL url) throws IOException, SAXException, ParserConfigurationException {
getParser().parse(new InputSource(url.openStream()));
// Reordering the list of items, first item parsed (most recent) -> last item in the list
Collections.reverse(mFeed.getItems());
mFeed.setURL(url);
if (mFeed.getHomePage() == null)
mFeed.setHomePage(url);
return mFeed;
}
private XMLReader getParser() throws SAXException, ParserConfigurationException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
return xr;
答案 0 :(得分:0)
您需要使用qName。
来自doc:
localName - 本地名称(不带前缀),如果没有执行命名空间处理,则为空字符串。
qName - 限定名称(带前缀),如果限定名称不可用,则为空字符串。
本地名称基本上剥离了名称空间,所以如果你愿意的话,你也可以做“缩略图”,它也会匹配。