我正在 Android Studio 中使用 XmlPullParser 构建一个 RSS 提要解析器。我想在我的应用程序中显示提要图像,但是我无法检索 <media:thumbnail>
标记中的 url 属性值。请注意:我只检索 <item>
中的内容。在做了大量研究之后,我了解到它与 XML 名称空间有关。我setNamespaceAware(true)
,运气不好。我的所有其他元素都被正确检索,但我的 imageLink 变量一直为空。我也试过设置
else if (name.equalsIgnoreCase("media:thumbnail")) {
imageLink = xmlPullParser.getattributeValue(null,"url");
}
到 "media:thumbnail"
或 "thumbnail"
。
RSS 提要链接:https://www.goal.com/feeds/en/news
XML 代码片段:
<item>
<title>
<![CDATA[Mexico boss Martino responds after USMNT's berhalter calls on referee to take control of Gold Cup final]]>
</title>
<pubdate>Sat,31 Jul 2021 20:30:03 GMT</pubdate>
<link>
<![CDATA[https://www.goal.com/en/news/mexico-boss-martino-responds-after-usmnts-berhalter-calls-on/1h5obazjxkl3c1hrevc53ovtfy]]>
</link>
<guid isPermaLink="false">urn:perform:article:1h5obazjxkl3c1hrevc53ovtfy</guid>
<description>
<![CDATA[The two managers went back and forth when discussing each team's ability to manipulate the match official]]>
</description>
<category>@Geo; Everywhere</category>
<category>@Targeting</category>
<category>@Google Newsstand</category>
<category>@Apple News</category>
<category>@News2021</category>
<category>@News - original</category>
<category>@Manager/coach quotes</category>
<media:content url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" type="image/jpeg" />
<media:thumbnail url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" />
<media:content url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" type="image/jpeg" />
<media:thumbnail url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" />
<media:content url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" type="image/jpeg" />
<media:thumbnail url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" />
<media:content url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" type="image/jpeg" />
<media:thumbnail url="http://images.daznservices.com/di/library/goaL/36/54/martino-berhalter-split_1bbaicsveobrw1xcg0fsjk04ra.png?t=43319131" />
<df:language>en</df:language>
<dc:language>en</dc:language>
<df:authors/>
<dc:creator/>
</item>
这是我的代码
public List<RssFeedmodel> parseFeed(InputStream inputStream) throws XmlPullParserException,IOException {
String title = null;
String link = null;
String description = null;
String imageLink = null;
boolean isItem = false;
List<RssFeedmodel> items = new ArrayList<>();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(inputStream,"UTF-8");
while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {
int eventType = xmlPullParser.getEventType();
String name = xmlPullParser.getName();
if (name == null)
continue;
if (eventType == XmlPullParser.END_TAG) {
if (name.equalsIgnoreCase("item")) {
isItem = false;
}
continue;
}
if (eventType == XmlPullParser.START_TAG) {
if (name.equalsIgnoreCase("item")) {
isItem = true;
continue;
}
}
Log.d("MyXmlParser","Parsing name ==> " + name);
String result = "";
if (xmlPullParser.next() == XmlPullParser.TEXT) {
result = xmlPullParser.getText();
xmlPullParser.nextTag();
}
if (name.equalsIgnoreCase("title")) {
title = result;
} else if (name.equalsIgnoreCase("link")) {
link = result;
} else if (name.equalsIgnoreCase("description")) {
description = result;
} else if (name.equalsIgnoreCase("thumbnail")) {//for image
imageLink = xmlPullParser.getattributeValue(null,"url");
}
if (title != null && link != null && description != null) {
if (isItem) {
RssFeedmodel item = new RssFeedmodel(title,link,description,imageLink);
items.add(item);
} else {
mFeedTitle = title;
mFeedLink = link;
mFeedDescription = description;
}
}
}
return items;
} finally {
inputStream.close();
}
}