anuragneo
anuragneo

Reputation: 61

SAX Parser returning empty string

I am trying to extract data from RSS feed. RSS link - http://www.thehindu.com/sport/?service=rss?

Here are my default handler's character method.

public void characters(char[] ch, int start, int length) {
    String text = "";
    for (int i=0; i<length; i++)
        text += ch[start+i];

}

When I try to print the 'text' for the description tag, it comes out to be empty. Is there an error with the above code or is it the RSS data format that's causing the problem??

Upvotes: 0

Views: 754

Answers (2)

rapha&#235;λ
rapha&#235;λ

Reputation: 6523

The characters method might be invoked multiple times for a single text node better use something like this:

private StringBuilder stringBuilder; // or Deque<StringBuilder> for nested elements

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

  if ("...".equals(qName)) {
      stringBuilder = new StringBuilder();
  }

}

public void characters(char ch[], int start, int length)  {
  if (stringBuilder != null)
     stringBuilder.append(ch, start, length);
}

public void endElement(String uri, String localName, String qName) {
  if ("...".equals(qName)){
    String s = stringBuilder.toString();
  }
  stringBuilder = null;
}

The ... is used for the value of the element containing the text node. Depending on you namespace use, you might have to use localName as apposed to qName)

Upvotes: 2

user1531971
user1531971

Reputation:

It isn't clear how we are getting to here from the SAX representation of the RSS; Or, for that matter, what you have done to validate that you got to the URL, fetched and parsed some RSS.

But this method seems to do what the Java API can do in a String constructor: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String%28char[],%20int,%20int%29

Upvotes: 0

Related Questions