java - SAX Parser characters method doesn't collect all content -
i'm using sax parser parse xml , working fine.
i have below tag in xml.
<value>•certass >> certass</value>
here expect '•certass >> certass' output. below code returns certass
. there issue special chars of value
tag?
public void characters(char[] buffer, int start, int length) { temp = new string(buffer, start, length); }
it not guaranteed characters()
method run once inside element.
if storing content in string
, , characters()
method happens run twice, content second run. second time characters method runs overwrite contents of temp
variable stored first time.
to remedy this, use stringbuilder
, append()
contents in characters()
, process contents in endelement()
. example:
defaulthandler handler = new defaulthandler() { private stringbuilder stringbuilder; @override public void startelement(string uri, string localname,string qname, attributes attributes) throws saxexception { stringbuilder = new stringbuilder(); } public void characters(char[] buffer, int start, int length) { stringbuilder.append(new string(buffer, start, length)); } public void endelement(string uri, string localname, string qname) throws saxexception { system.out.println(stringbuilder.tostring()); } };
parsing string
"<value>•certass >> certass</value>
" , handler above gives output:
?certass >> certass
i hope helps.
Comments
Post a Comment