How to parse an RSS feed and display it as a link in a Blackberry app?

I wanted to parse the XML feed and display it as links in a Blackberry application.

After searching on Google, I found out that I need to use a SAX parser. I did not find a good example.

For example, if I want to analyze a rss news feed from bbc.co.uk. How to do it. How to extract images from RSS feeds.

Please help, advise and guide me. SIA Thanks

+3
source share
1 answer

Let's say its rss xml twitter we are talking about:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" 
  xmlns:georss="http://www.georss.org/georss">
  <channel>
    <title>Twitter / LPProjekt</title>
    <link>http://twitter.com/LPProjekt</link>
    <atom:link type="application/rss+xml" 
      href="http://twitter.com/statuses/user_timeline/27756405.rss" rel="self"/>
    <description>Twitter updates from Linkin Park Projekt</description>
    <language>en-us</language>
    <ttl>40</ttl>
  <item>
    <title>LPProjekt: the instrumental from &quot;what ive done&quot;</title>
    <description>LPProjekt: the instrumental from &quot;</description>
    <pubDate>Sun, 07 Feb 2010 23:34:26 +0000</pubDate>
    <guid>http://twitter.com/LPProjekt/statuses/8784251683</guid>
    <link>http://twitter.com/LPProjekt/statuses/8784251683</link>
  </item>
..
  </channel>
</rss>

First of all, execute the handler for your xml to take the <title> and <link> value from <item> only:

class RSSHandler extends DefaultHandler {
    boolean isItem = false;
    boolean isTitle = false;
    boolean isLink = false;
    String[] title = new String[] {};
    String[] link = new String[] {};
    String value = "";

    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        if (!isItem) {
            if (name.equalsIgnoreCase("item"))
                isItem = true;
        } else {
            if (name.equalsIgnoreCase("title"))
                isTitle = true;
            if (name.equalsIgnoreCase("link"))
                isLink = true;
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (isTitle || isLink)
            value = value.concat(new String(ch, start, length));
    }

    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (isItem && name.equalsIgnoreCase("item"))
            isItem = false;
        if (isTitle && name.equalsIgnoreCase("title")) {
            isTitle = false;
            Arrays.add(title, value);
            value = "";
        }
        if (isLink && name.equalsIgnoreCase("link")) {
            isLink = false;
            Arrays.add(link, value);
            value = "";
        }
    }
}

, , InputStream HttpConnection SAXParser.parse:

static String[][] getURLFromRSS(String url) {
        InputStream is = null;
        HttpConnection connection = null;
        RSSHandler rssHandler = new RSSHandler();
        try {
            connection = (HttpConnection) Connector.open(url);
            is = connection.openInputStream();
            try {
                SAXParser parser = SAXParserFactory.newInstance()
                        .newSAXParser();
                parser.parse(is, rssHandler);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (connection != null)
                    connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String[][] result = new String[2][];
        result[0] = rssHandler.title;
        result[1] = rssHandler.link;
        return result;
    }

LabelField :

class LinkLabel extends LabelField
{
    String mUrl = "";
    public LinkLabel(String title, String url) {
        super(title, FOCUSABLE);
        mUrl = url;
    }
    protected boolean navigationClick(int status, int time) {
        Browser.getDefaultSession().displayPage(mUrl);
        return true;
    }
}

:

public Scr() {
    String rssUrl = "http://twitter.com/statuses/user_timeline/27756405.rss";
    String[][] urlData = getURLFromRSS(rssUrl);
    for (int i = 0; i < urlData.length; i++) {
        String title = urlData[0][i];
        String url = urlData[1][i];
        add(new LinkLabel(title, url));
    }
}

alt text http://img215.imageshack.us/img215/7583/rssurl.jpg

+7

Source: https://habr.com/ru/post/1736793/


All Articles