java - 来自 RSS 的图像 URL

标签 java android xml rss

我有一个显示 RSS 源的应用程序。我能够毫无问题地获取项目标题、日期和链接,但我正在尝试找出如何获取缩略图 URL。

编辑:我正在获取 URL,但我的 URL 字符串数组仅填充了第一个 URL。它只是在数组中存储了 22 次。

以下是带有图像 URL 的 Feed 中的 XML:

<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://kyfbnewsroom.com/wp-content/uploads/2012/09/Beltway-News-web-250x250.jpg" width="250" height="250"/>

这是我的代码:

public void getDataFromFeed(){

        try {
            Feed = new URL(URLFeed);
            DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
            db = dbf.newDocumentBuilder();
            doc = db.parse(new InputSource(Feed.openStream()));
            doc.getDocumentElement().normalize();
            nodeList = doc.getElementsByTagName("item");

            title = new String[nodeList.getLength()];
            pubDate = new String[nodeList.getLength()];
            link = new String[nodeList.getLength()];
            thumb = new String[nodeList.getLength()];

            for(int i=0;i<nodeList.getLength();i++){
                Node node = nodeList.item(i);

                Element fstElmnt = (Element) node;

                NodeList titleList = fstElmnt.getElementsByTagName("title");
                Element titleElement = (Element) titleList.item(0);
                titleList = titleElement.getChildNodes();
                title[i] = ((Node) titleList.item(0)).getNodeValue();

                NodeList pubDateList = fstElmnt.getElementsByTagName("pubDate");
                Element pubDateElement = (Element) pubDateList.item(0);
                pubDateList = pubDateElement.getChildNodes();
                pubDate[i] = ((Node) pubDateList.item(0)).getNodeValue();
                // chops off the " +0000" on date
                pubDate[i] = pubDate[i].substring(0, pubDate[i].length()-14);

                NodeList thumbList = doc.getDocumentElement().getElementsByTagName("media:thumbnail");
                Element thumbElement = (Element)thumbList.item(0);
                String thumbURL = thumbElement.getAttribute("url");
                thumb[i] = thumbURL;

                NodeList linkList = fstElmnt.getElementsByTagName("link");
                Element linkElement = (Element) linkList.item(0);
                linkList = linkElement.getChildNodes();
                link[i] = ((Node) linkList.item(0)).getNodeValue();
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

最佳答案

如果您使用 org.w3c.dom,请执行以下操作:

NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("media", "thumbnail");
// if you expect there to possibly be more than one use a loop or whatever
Element elm = (Element) nodes.item(0); 
String url = elm.getAttribute("url");

关于java - 来自 RSS 的图像 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25289539/

相关文章:

android - 如何计算 num_partition_sectors 和 start_sector 以从 android rom 的 system.img 创建 rawprogram0.xml 或 rawprogram_unsparse.xml?

java - 我应该在哪里存储我想在整个 Tomcat REST ws 中访问的变量?

java - 尝试设置 tomcat 以使用 servlet

java - jsp中的流数据显示

java - 我的两个 Java GUI 不会使用 setVisible(true) 显示,但其他三个会显示

android - 在 Android 中安排一周中的某些天的任务

android - 使用 dagger2 更改在应用程序类中初始化的改造 baseurl

如果没有 @Provides-annotated 方法,就无法提供 android.content.Context

javascript - 使用 XmlService 将自定义字符串解析为 XML 文档

java - 如何调用和显示