Comparison of several analytic XML methods of Android

Source: Internet
Author: User
Tags throwable

52371342

First, parsing XML using sax

SAX (Simple API for XML) uses streaming, and it does not record information about what is read. It is an event-driven xml API that resolves quickly and consumes less memory. Use a callback function to implement. The disadvantage is that it cannot be reversed.

Second, parsing XML using the DOM

The DOM is an object model for XML documents that can be used to directly access various parts of an XML document. It is all-in-one loading of content in memory, generating a tree structure that does not involve callbacks and complex state management. The disadvantage is inefficient when loading large documents.

III. parsing XML using pull

The pull is built into the Android system. It is also the way the official parsing layout files are used. Pull is a bit like sax and provides similar events, such as the start element and the end element. The difference is that the event driver for Sax is to callback the corresponding method, to provide a callback method, and then to automatically invoke the corresponding method within sax. The pull parser does not require a method to provide a trigger. Because the event he triggered is not a method, but a number. It is easy to use and highly efficient.

Iv. comparison of SAX, DOM, Pull:
    • Memory footprint: SAX, pull is better than Dom;
    • Programming: Sax uses event-driven, when the corresponding event triggers, will call the user-compiled method, that is, each parsing a class of XML, it is necessary to write a new suitable for the class XML processing class. Dom is the norm, pull concise.
    • Access and modification: Sax uses streaming parsing, Dom random access.
    • Access mode: Sax,pull parsing is synchronous, Dom word by word.
Iv. various analytical Examples 1, Sax parsing examples

Parsing code:

PublicClassSaxforhandlerExtendsDefaultHandler {/** *-----------------Sax parsing XML----------------------*/PrivateStaticFinal String TAG ="Saxforhandler";private list<person> persons;Private String Pertag;By this variable, the name of the previous label is recorded as person person;Record current personPublic list<person>Getpersons () {return persons; }@OverridePublicvoidCharacters (char[] CH,int start,int length)Throws Saxexception {String data =New String (CH, start, length). Trim ();if (!"". Equals (Data.trim ())) {LOG.I (TAG,"Content:" + Data.trim ());}if ("Name". Equals (Pertag)) {person.setname (data);}Elseif ("Age". Equals (Pertag)) {Person.setage (New short (data)); }Super.characters (CH, start, length); }@OverridePublicvoidEnddocument ()Throws Saxexception {log.i (TAG,"***enddocument () * * * *");Super.enddocument (); }@OverridePublicvoidEndElement (String uri, String localname, String qName)Throws Saxexception {log.i (TAG, QName +"***endelement () * * * *");if (' Person '. Equals (LocalName)) {Persons.add (person);Null } Pertag =NullSuper.endelement (URI, LocalName, QName); }@OverridePublicvoidStartdocument ()Throws Saxexception {persons =New Arraylist<person> (); LOG.I (TAG,"***startdocument () * * * *");Super.startdocument (); }@OverridePublicvoidStartelement (String uri, String localname, String qName, Attributes Attributes)throws saxexception {//localname tag name, fullname tag name with namespace, Attribute stores all properties of the tag if ( "person". Equals (LocalName)) {for (int i = 0; i < attributes.getlength (); i++) {log.i (TAG,  "AttributeName:" + attributes.getlocalname (i) +  "_attribute_value:" + attributes.getvalue (i)); person = new person (); Person.setid (integer.valueof (Attributes.getvalue (i))); //Person.setid (Integer.parseint (Attributes.getvalue (i))); }} Pertag = LocalName; LOG.I (TAG, qName +  "***startelement () * * *"); super.startelement (URI, LocalName, qName, Attributes);}}      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

Parsing with sax:

/** * ***************************** parsing an XML file using sax ******************* * Input stream is a pointer to the read data in the program *@throws Throwable * *PublicvoidTestsaxgetpersons ()throws throwable {inputstream InputStream = this.getClass (). getClassLoader (). getResourceAsStream ( "person.xml"); Saxforhandler Saxforhandler = new Saxforhandler (); /** * Factory mode parsing XML */saxparserfactory SPF = saxparserfactory.newinstance (); SAXParser saxparser = Spf.newsaxparser (); Saxparser.parse (InputStream, Saxforhandler); //the second way of parsing XML //XMLReader XMLReader = Saxparser.getxmlreader ( ); //Xmlreader.setcontenthandler (handler); //xmlreader.parse (New InputSource (InputStream)); list<person> persons = Saxforhandler.getpersons (); Inputstream.close (); for (person person:persons) {log.i (TAG, person.tostring ());}}     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
2. Dom parsing example
PublicClassDompersonservice {/** *@param instream *@return *@throws Exception * *PublicStatic list<person>Getpersons (InputStream instream)Throws Exception {list<person> persons =New Arraylist<person> ();/** * File parsing */documentbuilderfactory factory = Documentbuilderfactory.newinstance (); Documentbuilder builder = Factory.newdocumentbuilder (); Document document = Builder.parse (instream);/** * operation to Image tree */Element root = Document.getdocumentelement ();Returns the root element of the file NodeList personnodes = Root.getelementsbytagname ("Person");for (int i =0; I < personnodes.getlength (); i++) {Element personelement = (Element) personnodes.item (i);int id =New Integer (Personelement.getattribute ("id")); Person person =new person (); Person.setid (ID); NodeList childNodes = Personelement.getchildnodes (); for (int y = 0; y < Childnodes.getlength (); y++) {if (Childnodes.item (y). Getnodetype () = = Node.element_node) {if (" name ". Equals (Childnodes.item (y). Getnodename ())) {String name = Childnodes.item (y). Getfirstchild (). Getnodevalue (); Person.setname (name); } else if ( "age". Equals ( Childnodes.item (y). Getnodename ()) {String age = Childnodes.item (y). Getfirstchild (). Getnodevalue (); Person.setage ( Span class= "Hljs-keyword" >new short (age); }}} persons.add (person); } instream.close (); return persons;}           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

Parsing an XML file using the DOM

public void testDOMGetPersons() throws Throwable {    InputStream inStream = this.getClass().getClassLoader() .getResourceAsStream("person.xml"); List<Person> persons = DomPersonService.getPersons(inStream); for (Person person : persons) { Log.i(TAG, person.toString()); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
3, Pull Analysis example
PublicClassPullpersonservice {/** *------------------------using Pull to parse XML-----------------------*@param instream *@return *@throws Exception * *PublicStatic list<person>Getpersons (InputStream instream)Throws Exception {Person person =Null list<person> persons =Null Xmlpullparser Pullparser = Xml.newpullparser (); Pullparser.setinput (Instream,"UTF-8");int event = Pullparser.geteventtype ();The first event to be discoveredwhile (event! = xmlpullparser.end_document) {Switch (event) {Case XmlPullParser.START_DOCUMENT:persons =New Arraylist<person> ();BreakCase Xmlpullparser.start_tag:if ("Person". Equals (Pullparser.getname ())) {int id =New Integer (Pullparser.getattributevalue (0)); person = new person (); Person.setid (ID);} if (person! = null) {if ( "name". Equals (Pullparser.getname ())) {Person.setname (Pullparser.nexttext ());} if (new Short (Pullparser.nexttext ())); }} break; case xmlpullparser.end_tag: if ( " Person ". Equals (Pullparser.getname ())) {Persons.add (person), person = null;} break; } event = Pullparser.next (); } return persons;}}           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

Pull Parse XML file

public void testPullGetPersons() throws Throwable {    InputStream inStream = this.getClass().getClassLoader() .getResourceAsStream("person.xml"); List<Person> persons = PullPersonService.getPersons(inStream); for (Person person : persons) { Log.i(TAG, person.toString()); }}

Comparison of several analytic XML methods of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.