如何使用 XmlPullParser 解析命名空间以检索 <media:thumbnail> 标记中的 url 链接?

我正在 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();
    }
}
BMWV8 回答:如何使用 XmlPullParser 解析命名空间以检索 <media:thumbnail> 标记中的 url 链接?

只是回答我自己的问题以供将来参考:

public List<RssFeedModel> parseFeed(InputStream inputStream) throws XmlPullParserException,IOException {

/**
 * Declares the variables to hold article title,link and description
 * Declares List to hold all items once parsed
 */
String title = null;
String link = null;
String description = null;
String imageLink = null;
List<RssFeedModel> items = new ArrayList<>();

/**Logic for parsing through the XML*/
try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(inputStream,null);

    /**Retrieves eventType (START_DOCUMENT,START_TAG,END_DOCUMENT)*/
    int eventType = xpp.getEventType();
    /**Only checks items that are within the <item></item> tags*/
    boolean insideItem = false;

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            if (xpp.getName().equalsIgnoreCase("item")) {
                insideItem = true;
            } else if (xpp.getName().equalsIgnoreCase("title")) {
                if (insideItem) {
                    title = xpp.nextText();
                }
            } else if (xpp.getName().equalsIgnoreCase("description")) {
                if (insideItem) {
                    description = xpp.nextText();
                }
            } else if (xpp.getName().equalsIgnoreCase("link")) {
                if (insideItem) {
                    link = xpp.nextText();
                }
            } else if (xpp.getName().equalsIgnoreCase("thumbnail")) {
                if (insideItem) {
                    imageLink = xpp.getAttributeValue(null,"url");
                }
            }

            /**Once cursor arrives at the END_TAG,save parsed items into List*/
        } else if (eventType == XmlPullParser.END_TAG) {
            if(xpp.getName().equalsIgnoreCase("item")){
                if (insideItem) {
                    RssFeedModel item = new RssFeedModel(title,link,description,imageLink);
                    items.add(item);
                }else if (xpp.getName().equalsIgnoreCase("channel")){
                    insideItem = false;
                }
            }
        }
        eventType = xpp.next();
    }

    return items;
} finally {
    inputStream.close();
}

}

本文链接:https://www.f2er.com/3876.html

大家都在问