XML file parsing Summary detailed introduction to SAX/DOM/PULL

Source: Internet
Author: User
Tags protected constructor xml reader
Sax features (SAX is short for SimpleAPIforXML) 1. high resolution efficiency, less memory occupied 2. resolution can be stopped at any time. 3. the entire document cannot be loaded into memory. 4. the xml file cannot be written to xml5.SAX for parsing. if you have any questions or errors during event-driven reading, please comment on it or add me Penguin 1262135886. thank you for your support for the differences between SAX, DOM4J, and PULL parsing.

Sax features (short for Simple API for XML)

1. high resolution efficiency and low memory usage

2. resolution can be stopped at any time

3. the entire document cannot be loaded into the memory.

4. xml cannot be written.

5. the Event-driven method is used to parse xml files using SAX.

Differences between pull and sax

1. after pull reads the xml file, a number is returned when the corresponding event call method is triggered.

2. pull can be controlled in the program and can be stopped wherever it is parsed.

3. pull resolution is recommended for Android.

DOM features

Advantages

1. the entire document tree is in the memory for ease of operation. multiple features such as deletion, modification, and rescheduling are supported.

2. access xml documents through a tree structure

3. moving forward or backward on a node of the tree

Disadvantages

1. transfer the entire document to memory (including useless nodes), wasting time and space

Applicable scenarios

Once the documents are parsed, the data needs to be accessed multiple times. The Hardware Resources (memory and cpu) are sufficient)

First, define a Student. xml file.

** Example **

[Code]
 
     
          
   
    
Xiaohong
   21        
   
    
Female
   Shanghai    
      
          
   
    
Xiao Hei
   22        
   
    
Male
   Tianjin    
      
          
   
    
Small Network
   23        
   
    
Male
   Beijing    
  
 

** 1. sax parsing **

[Code] package com. example. sax_xml; import java. io. IOException; import java. io. inputStream; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. inputSource; import org. xml. sax. XMLReader; import android. app. activity; import android. content. res. assetManager; import android. OS. bundle; import android. view. view; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedIn StanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void sax_xml (View v) {// Get the Device manager object AssetManager manager = this. getAssets (); try {// Get Student under the assets directory. xml file input stream InputStream is = manager. open ("Student. xml ");/*** SAXParserFactory defines an API factory so that the application can configure and obtain a parser based on SAX (Simple API for * XML, to parse XML documents (original: Defines a factory API that enables * applications to configure and obtain a SAX based parser to parse * XML documents .) ** The constructor is Protected and can only use the newInstance () method to obtain the instance (Protected constructor to * force use of newInstance ().) */SAXParserFactory factory = SAXParserFactory. newInstance ();/*** XmlReader class is an abstract base class that provides non-cache and read-only access to XML data. This class complies with the W3C Extensible Markup Language (XML) * 1.0 and XML namespace recommendations. XmlReader supports reading XML data from streams or files. * The methods and attributes defined in this class allow you to browse data and read node content. The current node refers to the node where the reader is located. * Use any read methods and attributes that return the current node value to promote the reader. XmlReader class allows you to: 1. check whether the characters are legal * XML characters, and whether the element and attribute names are valid XML names. 2. check whether the XML document format is correct. 3. verify the data according to the DTD * or architecture. 4. retrieve data from an XML stream or skip unnecessary records using the extraction model. */XMLReader xmlReader = factory. newSAXParser (). getXMLReader ();/*** ContentHandler is a special SAX interface in a Java class package and is located in the org. xml. sax package. This interface encapsulates some Event Processing Methods *. when the XML parser begins to parse the XML input file, it will encounter some special events, such as the beginning and end of a document, the beginning and end of an element, and the character data in the element *. When these events occur, the XML parser calls the corresponding method in the ContentHandler interface to respond to the event. * /// Because it is an interface, I directly write a class that inherits its sub-class DefaultHandler and re-compile its method ContentHandler handler = new ContentHandler (); // Set The ContentHandler instance to XMLReader // setContentHandler this method sets the XML reader content handler xmlReader. setContentHandler (handler); // start parsing // InputSource: a single input source of an XML object. XmlReader. parse (new InputSource (is);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}

** Self-defined ContentHandler class **

Import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler; import android. util. log; public class ContentHandler extends DefaultHandler {private StringBuilder id; private StringBuilder name; private StringBuilder sex; private StringBuilder age; private StringBuilder adress; private String nodeName; // record the name of the current node // call @ Override public void startDocument () throws SAXException {id = new StringBuilder (); name = new StringBuilder () when starting xml parsing (); sex = new StringBuilder (); age = new StringBuilder (); adress = new StringBuilder ();} // call @ Override public void startElement (String uri, string localName, String qName, Attributes attributes) throws SAXException {nodeName = localName;} // call @ Override public void characters (char [] ch, int start, int length) throws SAXException {if ("id ". equals (nodeName) {id. append (ch, start, length);} else if ("name ". equals (nodeName) {name. append (ch, start, length);} else if ("age ". equals (nodeName) {age. append (ch, start, length);} else if ("sex ". equals (nodeName) {sex. append (ch, start, length);} else if ("adress ". equals (nodeName) {adress. append (ch, start, length) ;}// call @ Override public void endElement (String uri, String localName, String qName) when parsing a node) throws SAXException {if ("student ". equals (localName) {Log. d ("ContentHandler", "id is" + id. toString (). trim (); Log. d ("ContentHandler", "name is" + name. toString (). trim (); Log. d ("ContentHandler", "age is" + age. toString (). trim (); Log. d ("ContentHandler", "sex is" + sex. toString (). trim (); Log. d ("ContentHandler", "adress is" + adress. toString (). trim (); // Finally, clear the StringBuilder id. setLength (0); name. setLength (0); age. setLength (0); sex. setLength (0); adress. setLength (0) ;}/// call @ Override public void endDocument () throws SAXException when parsing the entire XML {// TODO Auto-generated method stub super. endDocument ();}}

** 2. pull parsing **

[Code] package com. example. xmlpull; import android. app. activity; import android. content. res. assetManager; import android. OS. bundle; import android. util. log; import android. util. xml; import android. view. view; import android. widget. toast; import org. xmlpull. v1.XmlPullParser; import java. io. inputStream; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map;/***** the number 0 START_DOCUMENT is returned for the declaration of reading xml; * The number 1 END_DOCUMENT is returned for reading the end of xml; * return number 2 START_TAG * return number 3 END_TAG * return number 4 TEXT **/public class MainActivity extends Activity {/*** load parsed data */private List
 
  
> OList; private Map
  
   
OMap; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void btn_pull (View v) {// Get the Device manager object AssetManager manager = this. getAssets (); try {// Get Student in the assets file. xml file input stream InputStream is = manager. open ("Student. xml "); // Get The pull parsing object. its constructor is protected. Therefore, the instance XmlPullParser parser = Xml can only be obtained using the newInstance () method. newPullP Arser (); // transmits the xml file input stream to the pull parsing object parser. setInput (is, "UTF-8"); // gets the event type for parsing, int type = parser. getEventType (); // use the while loop. if the event type parsed is not equal to the last node type of the entire document, the while (type! = XmlPullParser. END_DOCUMENT) {// Obtain the name of the current node String nodeName = parser. getName (); switch (type) {// if it is the start node type of the entire document, case XmlPullParser. START_DOCUMENT: // initialize the set of loaded data oList = new ArrayList
   
    
> (); Break; // if it is a group start node type case XmlPullParser. START_TAG: // Determine if ("students ". equals (nodeName) {} else if ("student ". equals (nodeName) {oMap = new HashMap
    
     
(); // Get the student node String id = parser starting with group. getAttributeValue (0); oMap. put ("id", id);} else if ("name ". equals (nodeName) {// the text String name of the node = parser. nextText (); oMap. put ("name", name);} else if ("sex ". equals (nodeName) {String sex = parser. nextText (); oMap. put ("sex", sex);} else if ("age ". equals (nodeName) {String age = parser. nextText (); oMap. put ("age", age);} else if ("adress ". equals (nodeName) {String adress = parser. nextText (); oMap. put ("adress", adress);} break; // the last node of the group, case XmlPullParser. END_TAG: if ("name ". equals (nodeName) {Toast. makeText (this, "name resolution completed", Toast. LENGTH_LONG ). show ();} if ("student ". equals (nodeName) {oList. add (oMap);} break;} // switch to the next group type = parser. next () ;}} catch (Exception e) {e. printStackTrace ();} // finally traverses the set Log for (int I = 0; I <oList. size (); I ++) {Log. e ("error", "name:" + oList. get (I ). get ("name") + "---- sex:" + oList. get (I ). get ("sex") + "---- age:" + oList. get (I ). get ("age") + "---- address:" + oList. get (I ). get ("adress "));}}}
    
   
  
 

First, let's take note of DOM parsing, because our teacher made this mistake when talking about it.

Here, when we get the node student, that is, the area drawn by the arrow in the figure, if we call its getChildNodes () method, how many subnodes do we have? It does not include its grandson node, except for Xiaohong because it is a grandson node. It has a total of five subnodes, marked as a black horizontal line in the figure. Therefore, when parsing, be careful not to ignore the blank space.

The specific parsing code is attached below

Here I split the dom parsing part into a tool class.

[Code] package com. example. domxml; import java. io. inputStream; import java. util. arrayList; import java. util. list; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList;/*** Dom parsing is to load all xml files and assemble them into a dom tree. * then, xml files are parsed through the relationship between nodes and nodes, open/public class Dom_xml_Util {private List at one layer
 
  
List = new ArrayList
  
   
(); Public List
   
    
GetStudents (InputStream in) throws Exception {// gets the dom resolution factory. its constructor is protected. Therefore, you can only use the newInstance () method to obtain the instance DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // use the currently configured parameters to create a new DocumentBuilder instance. // DocumentBuilder obtains DOM document instances from XML documents. // When this class is used, the application programmer can obtain a Document DocumentBuilder builder = factory from XML. newDocumentBuilder (); // Obtain Document document = builder. parse (in); // getDocumentElement () is a convenient property, this attribute allows you to directly access the child node of the document Element. // The Element interface indicates an element in the HTML or XML document. Element = document. getDocumentElement (); // return the NodeList bookNodes = element of all descendant Elements with the given tag name in document order. getElementsByTagName ("student"); // traverses the number of nodes in the NodeList // getLength () list for (int I = 0; I
    
     

Student. class

[code]package com.example.domxml;public class Student {    private int id;    private String name;    private int age;    private String sex;    private String address;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }}

Call in activity

Activity_main

[code]
          
            
             
              
               
       
      

MainActivity

[Code] package com. example. domxml; import java. io. IOException; import java. io. inputStream; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. content. res. assetManager; import android. view. menu; import android. view. view; import android. widget. textView; public class MainActivity extends Activity {private TextView TV _id, TV _name, TV _age, TV _sex, TV _adress; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV _id = (TextView) findViewById (R. id. TV _id); TV _name = (TextView) findViewById (R. id. TV _name); TV _age = (TextView) findViewById (R. id. TV _age); TV _sex = (TextView) findViewById (R. id. TV _sex); TV _adress = (TextView) findViewById (R. id. TV _adress);} public void bnt_parse (View v) {AssetManager manager = getAssets (); try {InputStream in = manager. open ("Student. xml "); List
      
       
OList = new ArrayList
       
        
(); Try {// return a collection of generic Student oList = new Dom_xml_Util (). getStudents (in);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();} // traverses the set and obtains the first group of data in the set for (int I = 0; I <oList. size (); I ++) {TV _id.setText (oList. get (0 ). getId (); TV _name.setText (oList. get (0 ). getName (); TV _age.setText (oList. get (0 ). getAge (); TV _sex.setText (oList. get (0 ). getSex (); TV _adress.setText (oList. get (0 ). getAddress () ;}} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
       
      


The above is the detailed introduction to the XML file parsing and summary of SAX/DOM/PULL. For more information, see other related articles in the first PHP community!

Related Article

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.