Sax is short for Simple API for XML. It provides an interface for parsing XML files in Android. If you want to parse an XML file, we can use the sax technology. In actual use, each character in the XML file is read one by one and corresponding events are initiated. That is to say, the sax technology is event-driven. For example, startdocument, startelement, characters, and endelement.
Instance source code:
1 Public List <person> getpersons (inputstream instream) Throws Throwable
2
3 {
4 Saxparserfactory factory = saxparserfactory. newinstance ();
5 Saxparser parser = factory. newsaxparser ();
6 // Customize the extension of the ulthandler subclass, override some of the methods we need to parse XML
7 Personparser = New Personparser ();
8 Parser. parse (instream, personparser );
9 // Close the input stream after it is used.
10 Instream. Close ();
11 Return Personparser. getpersons ();
12
13 }
Saxparserfactory introduction:
Defines an API factory for ApplicationsProgramYou can configure and obtain a simple API for XML
) To parse the XML document (Original: defines a factory API that enables applications to configure and obtain a sax based parser to parse XML documents .)
Its constructor is protected, so it can only use the newinstance () method to obtain the instance (protected constructor to force use of newinstance ().)
Saxparser introduction:
Defines an API inherited from the xmlreader class, and its constructor is also protected. The instance is obtained through the newsaxparser () method, various data sources can be used as XML for parsing (this method is public void parse (inputsource is, defaulthandler DH). These input data sources include input streams, files, URLs, And sax input resources. (Defines the API that wraps an xmlreader implementation class, an instance of this class can be obtained from the newsaxparser () method. once an instance of this class is obtained, XML can be parsed from a variety of input sources. these input sources are inputstreams, files, URLs, And sax inputsources .)
For more information about the use of sax, I will also write an article.