I’ve been googling around and reading on SO, but nothing worked. I have a problem with characters in an XML feed. I save the value of each tag in a String, but when occurs, it just stops. I only get the 4-5 first words in the tag or so.
So can anyone please help me with a method that can remove it? Or can it be that the text in the tags in the XML feed are too long for a String?
Thanks!
Sample code:
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("title"))
sitesList.setTitle(currentValue);
else if (localName.equalsIgnoreCase("id"))
sitesList.setId(currentValue);
else if(localName.equalsIgnoreCase("description"))
sitesList.setDescription(currentValue);
}
The text in the description tag is quite long, but I only get the first five words before the characters starts coming.
You’re using a SAXparser to parse the XML-String.
The
characters()-method can be called multiple times when only reading one XML-element. This happens when it finds something like<desc>blabla bla & # 39; bla bla la.</desc>.The solution is to use a
StringBuilderand append the readed characters in thecharacters()-method and then reset theStringBuilderin theendElement()-method:The above code works for me, given this XML-File:
The output is: