Method of parsing XML file using Sax in Android

Sax is an XML parser with fast parsing speed and low memory consumption. It is very suitable for mobile devices such as Android. Sax parsing XML files is event driven, that is, it does not need to parse a complete document. In the process of parsing documents in content order, Sax will judge whether the currently read characters are legal or not. If they meet, an event will be triggered. Events are actually callback methods defined in the contenthandler interface. The following are some common methods of the contenthandler interface:

Startdocument (): when the beginning of a document is encountered, this method is called to do some preprocessing work.

Enddocument (): corresponding to the above method, this method is called when the document ends, and you can do some aftercare work in it.

startElement(String namespaceURI,String localName,String qName,Attributes atts)

This method is triggered when a start tag is read. NamespaceURI is the namespace, localname is the tag name without namespace prefix, and QName is the tag name with namespace prefix. All attribute names and corresponding values can be obtained through ATTS. It should be noted that an important feature of Sax is its streaming processing. When a tag is encountered, it will not record the tags encountered before. That is, in the startelement () method, all the information you know is the name and attribute of the tag. As for the nested structure of the tag and the name of the upper tag, Whether there are sub elements and other structure related information is unknown and needs your program to complete it. This makes Sax less convenient in programming than dom.

Endelement (string URI, string name): this method corresponds to the above method. This method is called when an end tag is encountered.

Characters (char [] ch, int start, int length): this method is used to process the content read in the XML file. The first parameter is the string content of the file, and the last two parameters are the starting position and length of the read string in this array. You can obtain the content by using new string (CH, start, length).

Note: when there are too many characters between labels, or when the string contains special characters such as "\ n", characters will be lost. In case of this problem, do not doubt that Sax cannot parse XML with "\ n" or many characters in the tag. In fact, there is a problem with the handler we wrote. When Sax parses XML, it will call characters many times when it encounters more content in a tag. So we should consider this situation when writing the handler. You won't lose characters. The following is a solution: create a new temporary variable temp, use temp to receive data in calling the characters method, assign the temporary variable temp to the target variable in the endelement method, and empty the temporary variable temp to avoid dirty data. This can solve the problem that Sax cannot read the content behind the newline symbol when reading XML. Ljq.xml file

Operation results

In the Android simulator, if it is Chinese characters, there will be garbled code, which will not happen on the real machine

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>