Image URL from RSS Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Image URL from RSS without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have an app that shows an RSS feed. I am able to get the item titles, dates, and links with no problems but I am trying to figure out how to get the thumbnail URL.

EDIT: I am getting URLs, but my array of URL strings is filled with only the first URL. It is just storing it 22 times in the array.

Here is the XML from the feed with the image URL:

<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"/>

Here is my code:

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();
        }
    }

Answer

if you’re using org.w3c.dom do this:

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");
We are here to answer your question about Image URL from RSS - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji