来自RSS的图片网址

时间:2014-08-13 14:57:57

标签: java android xml rss

我有一个显示RSS Feed的应用。我可以毫无问题地获取项目标题,日期和链接,但我想弄清楚如何获取缩略图URL。

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

以下是带有图片网址的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();
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您正在使用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");