Notes for Java xml sax Parsing
As you can see from the Internet, all the sax Parsing is in the characters method of Handler to assign values to object data. The sample code is as follows: private TransportFile parseXML (String xml) {SAXParserFactory saxfac = SAXParserFactory. newInstance (); try {SAXParser saxparser = saxfac. newSAXParser (); InputStream is = new ByteArrayInputStream (xml. getBytes (); MySAXHandler handler = new MySAXHandler (); saxparser. parse (is, handler); return handler. getData ();} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (SAXException E) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null;} private class MySAXHandler extends DefaultHandler {String currentTagName = ""; TransportFile mData = null; @ Overridepublic void startElement (String uri, String localName, String qName, attributes attributes) throws SAXException {currentTagName = qName; if ("f Ile ". equals (qName) {mData = new TransportFile () ;}@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {String str = new String (ch, start, length); if ("guid ". equals (currentTagName) {mData. guid = str;} else if ("name ". equals (currentTagName) {mData. name = str;} else if ("type ". equals (currentTagName) {mData. type = str;} else if ("length ". equals (currentTagName) {mDa Ta. length = Long. parseLong (str);} else if ("index ". equals (currentTagName) {mData. index = Integer. parseInt (str);} else if ("count ". equals (currentTagName) {mData. count = Integer. parseInt (str);} else if ("data ". equals (currentTagName) {mData. data = Base64.decode (str) ;}@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {currentTagName = "";} public Trans PortFile getData () {return mData;} in common use scenarios, the above Code is correct. However, when the content of a label in xml is very long, the above Code bug will occur. In practice, we found that each time we parse the data with a length of about 1 kb, the excess part is actually parsed multiple times in segments. So the problem arises. If the characters method parses the data, the data in other segments will continue to overwrite the data in the final returned object, resulting in data loss. Therefore, the data assigned to the final object can only be spliced in the characters phase, and resolution must be completed in the endElement phase. Otherwise, when the data content is large, bugs in the online code will be highlighted. By the way, paste my code: private class MySAXHandler extends DefaultHandler {String currentTagName = ""; TransportFile mData = null; @ Override public void startElement (String uri, String localName, String qName, attributes attributes) throws SAXException {currentTagName = qName; mStringBuilder = new StringBuilder (); if ("file ". equals (qName) {mData = new TransportFile () ;}} private StringBuilder mStringBuilder; @ Override public void characters (char [] ch, int start, int length) throws SAXException {mStringBuilder. append (ch, start, length) ;}@ Override public void endElement (String uri, String localName, String qName) throws SAXException {String str = mStringBuilder. toString (); if ("guid ". equals (currentTagName) {mData. guid = str;} else if ("name ". equals (currentTagName) {mData. name = str;} else if ("type". Equals (currentTagName) {mData. type = str;} else if ("length ". equals (currentTagName) {mData. length = Long. parseLong (str);} else if ("index ". equals (currentTagName) {mData. index = Integer. parseInt (str);} else if ("count ". equals (currentTagName) {mData. count = Integer. parseInt (str);} else if ("data ". equals (currentTagName) {mData. data = Base64.decode (str);} currentTagName = "";} public TransportFile GetData () {return mData ;}} characters method parameters note that ch is the character array currently parsed and is not precise in the tag. The following is the output of ch, start, and length in characters when parsing the first tag: ============ characters ch: <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <File> <guid> 678c6f92-d617-40af-bb87-a80c3b2be91f </guid> <name> 0caq1_go.jpg </name> <type> image </type> <length> 71374 </length> <index> 0 </index> <count> 1 </count> <data>/9j/4 AAQSkZJRgABAQAAAQABAAD/issue/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoK ..... =========== characters start: 31 ================= characters length: 36. The data actually needed is from the start of the ch array. Length characters.