0

I am developing an RSS reader. When running this code, localName and uri empty. I am parsing a RSS feed. I am running the following code. The same code is working fine in another android project.

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    String name;
    if (localName == "" ){
        name = qName;
    }
    else{
        name = localName;
    }

    if (name.equalsIgnoreCase("title")) {
        currentPost.setTitle(chars.toString());
    }
    if (name.equalsIgnoreCase("link")) {
        currentPost.setLink(chars.toString());
    }
    if (name.equalsIgnoreCase("content")
            && currentPost.getContent() == null) {
        currentPost.setContent(chars.toString());
    }
    if (name.equalsIgnoreCase("item")) {
        currentPost.setFeed(feed);
        Posts.Instance().add(currentPost);
        currentPost = new Post();
    }
}
Charmi
  • 594
  • 1
  • 5
  • 20

1 Answers1

1

According to the API

Parameters:

uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.

...

and

The characters method can get called multiple times while inside a tag, especially if the element value contains whitespace.

In characters() docs

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

therefore,

When I write SAX parsers, I use a StringBuilder to append everything passed to characters():

public void characters (char ch[], int start, int length) {
    if (buf!=null) {
        for (int i=start; i<start+length; i++) {
            buf.append(ch[i]);
        }
    }
}

Then in endElement(), I take the contents of the StringBuilder and do something with it. That way, if the parser calls characters() several times, I don't miss anything.

Credit: https://stackoverflow.com/a/7182648/643500 and https://stackoverflow.com/a/2838338/643500

Edit::

Read http://sujitpal.blogspot.com/2008/07/rss-feed-client-in-java.html

Community
  • 1
  • 1
Sully
  • 14,672
  • 5
  • 54
  • 79
  • Thanks for these information. The same feed works fine in an android project but not in a classical java project. – Charmi May 24 '12 at 20:34