Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: SAX multiple calls to characters()

From: Sylvain Loiseau <sylvain@-----.--------->
To: NULL
Date: 8/5/2006 5:53:00 PM

Le 02-08-2006, metzger@r... <metzger@r...> a écrit :
> I am using the function listed below to handle characters events in
> SAX.  It does not handle multiple sequential calls to this function
> correctly.  
> For example, I am getting
> "2 4 816 32 64" as a value for an element when processing <vec> 2 4 8
> 16 32 64 </vec>
> because I am getting 2 calls to process the text in this element, one
> for "2 4 8" and the other for  "16 32 64".  

The expected behavior of a SAX API preserve all characters, whitespaces
included, of the input document. Your problem is either in your code, either
(less probably ;-)) in the SAX implementation you used.

> I have tried appending a
> blank to the result after each call to this function, but that
> sometimes splits numbers or words, depending on what is passed to this
> function.

I think this is definitly not the good solution :-)

> Is there a better way of handling multiple characters events? Thanks
> public void characters(char[] chars, int start, int length) {
>         while ( (length > 0) && Character.isWhitespace(chars[start]) )
> {
>             ++start;
>             --length;
>         }


>         while ( (length > 0) &&
> Character.isWhitespace(chars[start+length-1]) ) {
>             --length;
>         }

This is this piece of code, as far as I understand, which is responsible for
the behaviour you complain about! You remove the trailling whitespace
characters in the characters chunks you receive, so how can you expect to see
the whitespace characters in the outputed string?  

>         if ( length > 0 ) {
>            _text += new String(chars,start,length);
>         }
> }

Concatenation is dangerous for performance. You may consider using a
StringBuffer (sb.append(chars, start, length)). 


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent