Michael Stoner
Michael Stoner

Reputation: 1401

Why is Sax Parser not finding any elements?

I am having a heck of a time debugging the Android Sax wrapper classes. My question is 2 fold. 1. How do you debug using the Android Sax wrapper classes? 2. What am I doing wrong here?

I have a basic RSS reader that works fine. I am trying to create an Atom formatted reader. I have the following code.

    private List<Message> parseAtom()
    {
        final Message currentMessage = new Message();
        final List<Message> messages = new ArrayList<Message>();

        RootElement root = new RootElement("http://www.w3.org/2005/Atom", ATOM);
        Element title = root.getChild(TITLE);
        Element item = root.getChild(ENTRY);


        title.setEndTextElementListener(new EndTextElementListener() {

            public void end(String body)
            {
                Log.d("title", body);
            }
        });



        item.setEndElementListener(new EndElementListener() {
            public void end()
            {
                if (currentMessage.getAttribution() == null || currentMessage.getAttribution().length() == 0) currentMessage.setAttribution(Attribution);

                messages.add(currentMessage.copy());
            }
        });
        item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
            public void end(String body)
            {
                currentMessage.setTitle(body);
            }
        });
        item.getChild(ID).setEndTextElementListener(new EndTextElementListener() {
            public void end(String body)
            {
                currentMessage.setLink(body);
            }
        });
        item.getChild(SUMMARY).setEndTextElementListener(new EndTextElementListener() {
            public void end(String body)
            {
                currentMessage.setDescription(body);
            }
        });
        item.getChild(UPDATED).setEndTextElementListener(new EndTextElementListener() {
            public void end(String body)
            {
                currentMessage.setDate(body);
            }
        });


        item.getChild(Author).getChild(NAME).setEndTextElementListener(new EndTextElementListener() {
            public void end(String body)
            {
                currentMessage.setAttribution(body);
            }
        });



        try
        {
            String content = this.getInput();
            Xml.parse(content, root.getContentHandler());

            //Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        return messages;



    }

The constants in the getChild() calls are defined in the base class as:

static final String CHANNEL = "channel";
static final String PUB_DATE = "pubDate";
static final String DESCRIPTION = "description";
static final String LINK = "link";
static final String TITLE = "title";
static final String ITEM = "item";
static final String Author = "author";

static final String ENTRY = "entry";
static final String SUMMARY = "summary";
static final String ID = "id";
static final String UPDATED = "updated";
static final String NAME = "name";

The feed I was using as an example is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>NFL.com</title>
  <link rel="alternate" href="http://www.nfl.com/rss/rsslanding" />
  <link rel="self" href="http://www.nfl.com/rss/rsslanding" />
  <subtitle>Latest news from NFL.com for Atlanta Falcons</subtitle>
  <id>http://www.nfl.com/rss/rsslanding</id>
  <rights>© 2012 NFL Enterprises LLC</rights>
  <updated>2012-09-13T03:01:04Z</updated>
  <dc:date>2012-09-13T03:01:04Z</dc:date>
  <dc:rights>© 2012 NFL Enterprises LLC</dc:rights>
  <entry>
    <title>Brent Grimes to injured reserve with Achilles injury</title>
    <link rel="alternate" href="http://www.nfl.com/goto?id=0ap1000000060448" />
    <author>
      <name>Hanzus, Dan</name>
    </author>
    <id>http://www.nfl.com/goto?id=0ap1000000060448</id>
    <updated>2012-09-11T00:38:35Z</updated>
    <published>2012-09-10T18:50:00Z</published>
    <summary type="text">The Atlanta Falcons suffered a potentially devastating blow Monday with the news that cornerback Brent Grimes has played his last game of 2012. Who will step up in his absence?</summary>
    <dc:creator>Hanzus, Dan</dc:creator>
    <dc:date>2012-09-10T18:50:00Z</dc:date>
  </entry>
</feed>

When this runs, the Xml.parse(content, root.getContentHandler()); returns without appearing to do anything. It does not throw an exception. But none of the Listeners are ever hit (verified through breakpoints). The parser isn't throwing anything in logcat.

I added the title on the root element to see if I could hit that, but no go.

I originally had a problem with the parser throwing an error because of the namespace not being present (was matching feed it was given http://www.w3.org/2005/Atom:feed from the xml doc...caused error). After adding the namespace, it no longer throws the error, but it doesn't appear to be doing anything.

This is my first attempt at using the SAX wrappers (or sax for that matter). Am I missing something simple?

Upvotes: 0

Views: 636

Answers (1)

StackJP
StackJP

Reputation: 1470

I ran into this same issue recently when I was trying to parse some XML using this method and it took me awhile to figure it out but the problem was that I was leaving out the namespace in the child lookup. So in your reader instead of

Element title = root.getChild(TITLE);

use

Element title = root.getChild(NAMESPACE, TITLE);

You'll have to do this everytime you use getChild() in your reader.

Upvotes: 2

Related Questions