Instance Resolution Android Method for Resolving XML Using Pull Resolver

  • 2021-06-29 12:06:21
  • OfStack

1. Introduction to Pull
The Pull parser is built into the Android system. The Pull parser is similar to the SAX parser in that it provides similar events, such as the start element and the event introducing the element. With parser.next(), you can enter the next element and trigger the corresponding event, then process it accordingly, and when the element starts parsing,The value of the next Text type element can be obtained by calling the perser.nextText() method.

2. pull features
(1) Simple structure, 1 interface, 1 additional plant to form the Pull parser
(2) Simple and easy to use, the Pull parser only has one important method, next(), which is used to retrieve the next event, and his events only have five, START_DOCUMENT, START_TAG, TEXT, END_TAG, END_DOCUMENT
(3) Minimum memory consumption, like Pull parser and SAX parser 1, temporary memory usage is low, but SAX parsing is a bit cumbersome, DOM consumes memory, so Pull is recommended


3. src structure
The project package is named com.pullxml.mypull and the person.xml file exists in the src root directory


-- com.pullxml.util
-- -- Person.java
-- com.pullxml.mypull
-- -- MainAcitivity.java
-- com.pullxml.service
-- -- PullService.java
-- com.pullxml.test
-- -- PullTester.java
-- person.xml

4. Example Pull resolves XML
First create a new android.xml in the src directory


<?xml version="1.0" encoding="UTF-8"?> 
<persons> 
  <person id="23"> 
    <name>xiaanming</name> 
    <age>23</age> 
  </person> 
  <person id="20"> 
    <name>liudehua</name> 
    <age>28</age> 
  </person> 
</persons> 

Create a new PullXMLService


package com.example.pull_parser; 
 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.xmlpull.v1.XmlPullParser; 
 
import android.util.Log; 
import android.util.Xml; 
 
public class PullXMLService { 
  public static List<Person> readXML() throws Exception{ 
    // Obtain src Below the Catalog android.xml Input stream of file  
    InputStream is = PullXMLService.class.getClassLoader().getResourceAsStream("android.xml"); 
    // For storing parsed Person object  
    List<Person> persons = null; 
    //1 Tags  
    boolean flag = false; 
    Person person = null; 
     
    // instantiation 1 individual XmlPullParser object  
    XmlPullParser parser = Xml.newPullParser(); 
     
    // Setting input stream and encoding  
    parser.setInput(is, "UTF-8"); 
     
    // Triggered the 1 Events, based on XML The grammar of, that is, he began to understand the document  
    int eventCode = parser.getEventType(); 
     
    // If the resulting event code is the end of the document, the parsing ends  
    while (eventCode != XmlPullParser.END_DOCUMENT) { 
      switch(eventCode){ 
      case XmlPullParser.START_DOCUMENT:{ 
        // We started parsing 1 Do as usual 1 Some initialized operations  
        persons = new ArrayList<Person>(); 
        break; 
      } 
      case XmlPullParser.START_TAG:{ 
        // Determine if the current element is one that needs to be retrieved  
        if("person".equals(parser.getName())){ 
          flag = true; 
          person = new Person(); 
          person.setId(Integer.valueOf(parser.getAttributeValue(0))); 
        } 
        if(flag){ 
          if("name".equals(parser.getName())){ 
            person.setName(parser.nextText()); 
          }else if("age".equals(parser.getName())){ 
            person.setAge(Integer.valueOf(parser.nextText())); 
          } 
        } 
        break; 
      } 
      case XmlPullParser.END_TAG:{ 
        if("person".equals(parser.getName()) && person != null){ 
          flag = false; 
          persons.add(person); 
          Log.e("log", person.toString()); 
          person = null; 
        } 
        break; 
      } 
      } 
       
      // this 1 The step is important, and this method returns 1 Event codes, also triggered 1 Method of events  
      eventCode = parser.next(); 
    } 
     
    return persons; 
     
  } 
} 


Related articles: