简体   繁体   中英

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??

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM