Android Sax parsing xml two times parsing-------correcting our mistaken perceptions

Source: Internet
Author: User

Problem: A label resolves two times

Stickers for the card bar:


The number of targetPositionX0 and targetPositionY0 in the XML file is exactly the same, but when parsing, Y0 has been parsed two times. followed by the number of characters (not affected).

Online about how to parse XML with sax everywhere, I also mentioned in the previous article, but after today's problem, only know that the previous method is not complete. Below

Let's say the reason why the problem arises.


Sax can parse only about 2k files at a time, so when the contents of the label exceed this size, a second parse is required . reflected in the characters () function to take the label inside

Can not be completed at one time, so the above problems occur. Let's take a look at this diagram.


Explanation: DD is written in the EndElement () function, and we can see that the execution of the two characters executes only after it has been completed.


The correct parsing method:

 Private StringBuilder sb = new StringBuilder (); @Override public void characters (char[] ch, int start, int length) throws Saxexception {super.                    Characters (ch, start, length);      Save content to SB sb.append (CH, start, length);      } @Override public void Startdocument () throws Saxexception {super.startdocument ();  } @Override public void Startelement (string uri, String localname, String qName, Attributes Attributes) throws          saxexception {super.startelement (URI, LocalName, qName, attributes);      Start a new tab to clear historical data first sb.setlength (0); } @Override public void EndElement (string uri, String localname, String qName) throws Saxexce                    ption {super.endelement (URI, LocalName, QName);                   (4) The original value in the characters, is now changed in this value String value = sb.tostring (); Do the appropriate processing} @Override public void EnddocumENT () throws saxexception {super.enddocument ();   }  }


Android Sax parsing xml two times parsing-------correcting our mistaken perceptions

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.