Question

I'm trying to make an rss reader using the xmlpullparser. Let's say I have an xml file like this:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
 <channel>
 <title>RSS Title</title>
 <description>This is an example of an RSS feed</description>
 <link>http://www.someexamplerssdomain.com/main.html</link>
 <lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
 <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>
 <ttl>1800</ttl>

 <item>
  <title>Example entry</title>
  <description>Here is some text containing an interesting description.</description>
  <link>http://www.wikipedia.org/</link>
  <guid>unique string per item</guid>
  <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>
 </item>

 </channel>
</rss>

When I try reading it, it reads the rss tag and then, before reading channel, it reads a null. Moving the channel tag next to the rss tag, like this:

<rss version="2.0"><channel>
<title>...

I don't have this problem anymore, but i can't do this because i don't wanna read only my xml files.

This is part of the code i'm using:

public Feed rss() throws XmlPullParserException, IOException {
        matchStart(Tag.rss);
        move();
        Feed channel = channel();
        matchEnd(Tag.rss);

        return channel;
    }

    private Feed channel() throws XmlPullParserException, IOException {
        matchStart(Tag.channel);
        move();

        String title = null;
        String link = null;
        String description = null;
        String pubDate = null;
        List<FeedMessage> items = new ArrayList<FeedMessage>();

        while(eventType != XmlPullParser.END_TAG) {
            if(xmlParser.getEventType() != XmlPullParser.START_TAG) {   //Look if we are checking an open tag <tag> if not es</tag> then go to the next one
                move();
                continue;
            }

            String name = xmlParser.getName();
            switch(name) {
                case Tag.title: title = title(); break;
                case Tag.link: link = link(); break;
                case Tag.description: description = description(); break;
                case Tag.pubDate: pubDate = pubDate(); break;
                case Tag.item: items.add(item()); break;
                default: skip(); break;
            }

            move();
        }

        Feed channel = new Feed(title, link, description, pubDate, items);

        matchEnd(Tag.channel);

        return channel;
    }

where matchStart and matchEnd call xmlParser.require to check the START_TAG and END_TAG and move it's defined like this:

void move() throws XmlPullParserException, IOException {
        eventType = xmlParser.next();
    }

I can't find where the problem is, there something wrong with the code or the logic?

Was it helpful?

Solution

When you have a newline the next event after the opening rss tag is not the channel tag but a TEXT event representing this newline and the indentation before the channel tag. You need to keep skipping until you hit the next start tag event after the <rss>, just like you're already doing within the main loop inside channel().

OTHER TIPS

try this code

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();
        factory.setNamespaceAware(false);
        xpp.setInput(new StringReader(xml));

        int eventType = xpp.getEventType();
        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)
                    {
                   String s=xpp.nextText();
                   System.out.println("-----item title"+s);
                    normalHeadlines.add(s); // extract the// headline
                    }
                } 
                else if (xpp.getName().equalsIgnoreCase("link")) 
                {
                    if (insideItem)
                        normallinks.add(xpp.nextText()); // extract the link of
                                                    // article
                }
                else if (xpp.getName().equalsIgnoreCase("pubDate")) 
                {
                    if (insideItem)
                        normaldates.add(xpp.nextText()); // extract the link of
                                                    // article
                }

                else  if(xpp.getName().equalsIgnoreCase("description"))
                {
                    if (insideItem)
                    {
                    String s=xpp.nextText();
                    normaldescription.add(s);
                    separationImage(s);                 
                    }
                }
            } 
            else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) 
            {
                insideItem = false;
            }

            eventType = xpp.next(); // move to next element
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top