2

Had to reword some methods because my partner is paranoid, lol. If you see any typos, it may be because of that, but please still point them out if you think they may be causing the issue

Using XmlPullParser and at line 80, the value is always equal to 'Channel'. When it calls skip, parser.next() is always equal to 4. This code used to work (put it aside due to school) and now it returns nothing. It seems to be skipping all inner tags.

Any ideas why?

UPDATE: Looks like I'm getting a org.xmlpull.v1.XmlPullParserException. When I print the exception to the log, I get this the following result. I think it's saying it's looking for a start tag but it's returning null(?):

07-17 00:02:14.564: E/XmlPullParserException(13604): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}channel (position:START_TAG <rss xmlns:a10='http://www.w3.org/2005/Atom' version='2.0'>@2:60 in java.io.InputStreamReader@4259cca8) 

Here's the Parser class:

    import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;

public class Parser {

    // XML node keys
    private final String KEY_ITEM = "item"; // parent node
    private final String KEY_GUID = "guid";
    private final String KEY_LINK = "link";
    private final String KEY_TITLE = "title";
    private final String KEY_DESCRIPTION = "description";
    private final String KEY_UPDATED = "updated";

    public static final String nameSpace = null;


    public void parse(InputStream in, String type)
            throws XmlPullParserException, IOException {

        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            readFeed(parser, type);
        } finally {
            in.close();
        }
    }

    private void readFeed(XmlPullParser parser, String type)
            throws XmlPullParserException, IOException {

        parser.require(XmlPullParser.START_TAG, nameSpace, null);

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName(); // This is line 80
            Log.e("READFEED", String.valueOf(name));
            if (name.equals(KEY_ITEM)) {
                DatabaseManager.sInstance.addItem(readItem(parser, type));
            } else {
                skip(parser);
            }
        }
    }

    private Item readItem(XmlPullParser parser, String sportType)
            throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, nameSpace, KEY_ITEM);
        Log.e("readItem", "readItem");

        long id = 0;
        String link = null;
        int linkType = 0;
        String title = null;
        String description = null;
        String updated = null;

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            Log.e("readItem", name);

            if (name.equalsIgnoreCase(KEY_LINK)) {
                link = readTag(parser, KEY_LINK);
                if (link.contains("player")) {
                    linkType = 1;
                }
            } else if (name.equalsIgnoreCase(KEY_TITLE)) {
                title = readTag(parser, KEY_TITLE);
            } else if (name.equalsIgnoreCase(KEY_DESCRIPTION)) {
                description = readTag(parser, KEY_DESCRIPTION);
            } else if (name.equalsIgnoreCase(KEY_GUID)) {
                id = Long.valueOf(readTag(parser, KEY_GUID).replace(" ", ""));
            } else if (name.equalsIgnoreCase(KEY_UPDATED)) {
                updated = readTag(parser, KEY_UPDATED);
                Pattern regex = Pattern.compile("T*");
                Matcher regexMatcher = regex.matcher(updated);
                if (regexMatcher.find()) {
                    String date = regexMatcher.group();
                }
            } else {
                skip(parser);
            }
        }

        return DatabaseManager.sInstance.newItem(id, sportType, link, linkType, title,
                description, updated);
    }

    // Processes description tags in the feed.
    private String readTag(XmlPullParser parser, String tag)
            throws IOException, XmlPullParserException {

        parser.require(XmlPullParser.START_TAG, nameSpace, tag);
        String text = readText(parser);
        parser.require(XmlPullParser.END_TAG, nameSpace, tag);
        return text;
    }

    // For the tags title and description, extracts their text values.
    private String readText(XmlPullParser parser) throws IOException,
            XmlPullParserException {

        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

    private void skip(XmlPullParser parser) throws XmlPullParserException,
            IOException {

        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
    }
}

And here's the structure of the XML the code is pulling from

<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title></title>
        <link></link>
        <description></description>
        <language></language>
        <lastBuildDate></lastBuildDate>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
    </channel>
</rss>

Thanks all in advance :)

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Psest328
  • 6,575
  • 11
  • 55
  • 90

1 Answers1

3

The xml with my own sample data

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>Title</title>
        <link>Title Link</link>
        <description>Description</description>
        <language>Language</language>
        <lastBuildDate>Build</lastBuildDate>
        <item>
            <guid isPermaLink="false">Id1</guid>
            <link>Link1</link>
            <title>Title1</title>
            <description>description1</description>
            <a10:updated>updated1</a10:updated>
        </item>
        <item>
            <guid isPermaLink="false">Id2</guid>
            <link>Link2</link>
            <title>Title2</title>
            <description>Description2</description>
            <a10:updated>updated2</a10:updated>
        </item>
        <item>
            <guid isPermaLink="false">Id3</guid>
            <link>Link3</link>
            <title>Title3</title>
            <description>Description3</description>
            <a10:updated>updated3</a10:updated>
        </item>
        <item>
            <guid isPermaLink="false">Id4</guid>
            <link>Link4</link>
            <title>Title4</title>
            <description>Descriptiob4</description>
            <a10:updated>updated4</a10:updated>
        </item>
    </channel>
</rss> 

Here's the parsing smaple

public void parse(InputStream is)
  {
      try
      {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(false);
      XmlPullParser xpp = factory.newPullParser();
      xpp.setInput(is,null);

      boolean insideItem = false;

      // Returns the type of current event: START_TAG, END_TAG, etc..
      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("guid")) {
                  if (insideItem)
                      Log.i("Guid is",xpp.nextText()); 
              } else if (xpp.getName().equalsIgnoreCase("link")) {
                  if (insideItem)
                      Log.i("Link is",xpp.nextText());  
              }
              else if (xpp.getName().equalsIgnoreCase("title")) {
                  if (insideItem)
                      Log.i("Title is.",xpp.nextText());  
              }    
              else if (xpp.getName().equalsIgnoreCase("description")) {
                  if (insideItem)
                      Log.i("Description is.",xpp.nextText());  
              }
              else if (xpp.getName().equalsIgnoreCase("a10:updated")) {
                  if (insideItem)
                      Log.i("Updated url is.",xpp.nextText());  
              }


          } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
              insideItem = false;
          }

          eventType = xpp.next(); /// move to next element
      }
      }catch(Exception e)
      {
          e.printStackTrace();
      }
  }

The log

07-17 09:30:09.165: I/Guid is(11781): Id1
07-17 09:30:09.165: I/Link is(11781): Link1
07-17 09:30:09.165: I/Title is.(11781): Title1
07-17 09:30:09.165: I/Description is.(11781): description1
07-17 09:30:09.165: I/Updated url is.(11781): updated1
07-17 09:30:09.165: I/Guid is(11781): Id2
07-17 09:30:09.165: I/Link is(11781): Link2
07-17 09:30:09.165: I/Title is.(11781): Title2
07-17 09:30:09.165: I/Description is.(11781): Description2
07-17 09:30:09.165: I/Updated url is.(11781): updated2
07-17 09:30:09.165: I/Guid is(11781): Id3
07-17 09:30:09.165: I/Link is(11781): Link3
07-17 09:30:09.165: I/Title is.(11781): Title3
07-17 09:30:09.165: I/Description is.(11781): Description3
07-17 09:30:09.170: I/Updated url is.(11781): updated3
07-17 09:30:09.170: I/Guid is(11781): Id4
07-17 09:30:09.170: I/Link is(11781): Link4
07-17 09:30:09.170: I/Title is.(11781): Title4
07-17 09:30:09.170: I/Description is.(11781): Descriptiob4
07-17 09:30:09.170: I/Updated url is.(11781): updated4

Note: parsed all data with item tag only

Edit :

You have

parser.require(XmlPullParser.START_TAG, nameSpace, null);

Which should be

parser.require(XmlPullParser.START_TAG, nameSpace, "rss");
Raghunandan
  • 132,755
  • 26
  • 225
  • 256