Use of XML file parsing for Android Development

Foreword

This article mainly introduces how to parse XML files in Android. Sax mechanism is mainly adopted. Sax is fully called simple API for XML. It is not only an interface, but also a software package. As an interface, Sax is a standard interface for event driven XML parsing. There are generally two methods for parsing XML files, DOM and Sax. DOM needs to read all XML documents into the computer memory first. When the content of the document is too large, this method is not applicable. Sax solves this problem better. It is parsed line by line and can be interrupted at any time. But the operation of Sax is complex. Therefore, these two methods have their own advantages and disadvantages, depending on the specific application. In the previous article QT learning road_ 12 (simple data management system) uses the DOM method in QT.

Experimental description

Most Sax implementations generate events similar to the following:

Triggering a document processing event at the beginning and end of the document; Trigger element events before and after the end of parsing of each XML element in the document; Any metadata is usually delivered as a separate event for soybean oil; Generating a DTD or schema event when processing the DTD or schema of a document; An error event is generated to notify the host application of a parsing error.

The schematic diagram of Sax model is as follows:

If you want to use Sax to parse XML documents, you need a class to inherit the contenthandler class provided by the Android system. However, if you inherit the contenthandler class, even if you do not use all the methods provided by this class, you must implement all its internal methods (generally, unused methods can be directly replaced by empty methods), but it is not very convenient to develop in this way. Therefore, we can inherit the defaulthandler class instead. In this way, we only need to implement the methods required in the program. Other methods in this class have been replaced by empty methods.

The contenthandler interface has the following methods:

void startDocument();// Execute at the beginning of document parsing

void endDocument();// Execute at the end of document parsing

void startElement(String uri,String localName,String qName,Attributes atts);// Execute when label parsing starts

void endElement(String uri,Attributes atts);// Execute at the end of label resolution

void characters(char[] ch,int start,int length );// Execute when resolving label properties

When using Sax in Android to parse XML files, you need to establish a Sax factory, that is, saxparserfactory object, and an xmlreader object. This class is bound to the subclass of contenthandler and combined with the XML source file. That is, the processing process is to create an event handler, create a Sax parser, assign the key event handler to the parser, parse the document, and send each event to the handler.

The equal method of string class is used to judge whether the values of string type are equal.

This experiment is to parse a piece of XML code, and then print some content in each parsing function above. Mainly learn how to use Sax model to create parser and how to use the function of contenthandler subclass to parse.

Code of main part of experiment:

Mainactivity.java:

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