Java example: sax characters (char ch [], int start, int length)

Source: Internet
Author: User

Sax characters (char ch [], int start, int length) does not necessarily completely represent the value of an element. if the length of an XML document is too long, the value of an element may be read twice. Therefore, if the preceding method is used only to obtain the value of an element, possible incomplete data.
Sax may easily ignore some details about the characters callback method. characters will call the method multiple times between startelement and endelement, and cannot guarantee that the entire text block will be returned at one time, therefore, you must wait for the endelement to determine that the text node ends.
1. case:
<Line> I Love You, why I have no girlfriend </line>
The processing result in defaulthandler may be as follows:
Startelement: Line
Characters: I love you, why
Characters: I have no girlfriend
Endelement: Line

2. cause:
SAX Parser reads streams in blocks. By default, it reads 2 K bytes at a time. (CH contains not only the value information of the element, but also a part of the entire XML document. That is to say, the analyzer reads 2 K bytes from the document each time and puts them in this array, then, use start and length to divide the part of the value. in this case, the value of the last element cannot be completely included in this ch ). therefore, we still need to perform some additional operations to obtain the complete element value through characters (char ch [], int start, int length. parser reads 2 K Bytes each time to fill the ch.
The filling sequence overwrites the original values from the beginning to the end. Therefore, when reading the document for the last time, the remaining part may be less than 2 K, so ch appears, the first half is the end of the document, while the second half is redundant. This is not covered in the second last ch.
3. solution:
So you should use stringbuilder to cache,
Call sb. setlength (0) during startelement );
Call sb. append (CH, start, length) each time characters );
Call string STR = sb. tostring () only when endelement is enabled ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.