Android XML file parsing

On the Android platform, you can use the simple API for XML (sax), document object model (DOM) and the pull parser attached to Android to parse XML files. The following is the XML file to be parsed in this example: File Name: itcast.xml. The example defines a JavaBean to store the XML content parsed above. This JavaBean is person. JavaBean is a model in MVC design model, also known as model layer. In general programs, we call it data layer, which is used to set data properties and some behaviors. Then I will provide get / set methods to obtain and set properties. Sax is an XML parser with fast parsing speed and low memory consumption, which 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. Here are some methods commonly used by the contenthandler interface: startdocument() when it encounters the beginning of the document, call this method to do some preprocessing work. Enddocument () corresponds to the above method. When the document ends, call this method to do some aftercare work. Startelement (string NamespaceURI, string localname, string QName, attributes attributes) will trigger this method 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. The endelement (string URI, string localname, string name) 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. The content can be obtained by using new string (CH, start, length). As long as sax is provided with a class that implements the contenthandler interface, the class can be notified of events (in fact, Sax calls the callback method in this class). Because contenthandler is an interface, it may be inconvenient to use. Therefore, Sax also formulates a helper class for it: defaulthandler, which implements the contenthandler interface, but all its method bodies are empty. When implementing, you only need to inherit this class and rewrite the corresponding methods. Let's look at specific examples: first, create an itcast.xml file in the SRC directory, then create a cn.itcast.domain package, and create a person class under the package, which is mainly used to store the XML content parsed above. JavaBean is a model in the MVC design model, also known as the model layer. In general programs, we call it the data layer, It is used to set data properties and some behaviors, and then I will provide get / set methods to get properties and set properties

Reproduced at: https://www.cnblogs.com/ITlearning/p/3144680.html

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
分享
二维码
< <上一篇
下一篇>>