Reputation: 5400
I have an xml that i am parsing there is a field that contains very long text, but somehow it is dropped by the parser, is it so because i am using just string to get those character to should i use string buffer or builder. What i want is to extract value by tag only.
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
// its not reading element value, though tag is read.
elementValue = new String(ch, start, length);
elementOn = false;
}
}
This is the text:
<description>
<![CDATA[
Former CPI MLA Manish Kunjam, who returned on Thursday after, theis is a long long text very long that its being dropped.......so what can be done...................................................................................................
]]>
</description>
Thanks plz guide me.....
Upvotes: 0
Views: 1571
Reputation: 63955
Yes, use a StringBuilder since text can be read in chunks and you may be reading just the first empty line of code like this. See documentation
You can reset the StringBuilder each time you hit startElement
private final StringBuilder mStringBuilder = new StringBuilder();
private String elementValue = null;
private boolean elementOn = false;
public final void characters(char[] ch, int start, int length) throws SAXException {
if (elementOn)
mStringBuilder.append(ch, start, length);
}
public final void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
mStringBuilder.setLength(0);
if (someCondition) elementOn = true;
}
public void endElement(String uri, String localName, String qName) throws SAXException {
elementValue = mStringBuilder.toString().trim();
elementOn = false;
}
Parser does about the following
<description> -> startElement("description"), reset StringBuilder
<![CDATA[ -> characters(""), appended to (empty) String
Former CPI.. -> characters("Former.."), appended
]]> -> characters(""), appended
</description> -> endElement("description"), read text here
<description> -> startElement("description"), reset StringBuilder all starts again
Upvotes: 2