In Android, the pull parser is used to generate XML files and read XML files.

Source: Internet
Author: User

In Android, the pull parser is used to generate XML files and read XML files.

 

In Android, the Pull parser 1 is used, and the running mode of the Pull parser is similar to that of the SAX Parser. It provides similar events, such as the start element and end element events. 2. Use parser. next () to enter the next element and trigger the corresponding event. 3. The event is sent as an int value. Therefore, you can use a switch to process the event. 4. When parsing an element, call the parser. nextText () method to obtain the value of the next Text node. 5. Related API: Get the current node event type: parser. getEventType (); get the next node event type: parser. next (); get the tag property value: parser. getAttributeValue (); get the text after the tag: parser. nextText (); example:
 
 
  
   
    
Allen
   36
  
  
   
    
James
   25
  
 // Use the Pull parser to generate an XML file and write it to the local private void writeXmlToLocal () {List
 
  
PersonList = getPersonList (); // get the serialized object XmlSerializer serializer = Xml. newSerializer (); // XmlSerializer serializer = XmlPullParserFactory. newInstance (). newSerializer (); try {File path = new File (Environment. getExternalStorageDirectory (), "persons. xml "); FileOutputStream fos = new FileOutputStream (path); // specify the output location of the serialized object and the encoding serializer. setOutput (fos, "UTF-8"); // Write Start
  Serializer. startDocument ("UTF-8", true); serializer. startTag (null, "persons ");//
  
   
For (Person person: personList) {// start to write personserializer. startTag (null, "person ");//
   
    
Serializer. attribute (null, "id", String. valueOf (person. getId (); // write the nameserializer. startTag (null, "name") of person ");//
    
     
Serializer. text (person. getName (); serializer. endTag (null, "name ");//
    // Write the person's ageserializer. startTag (null, "age"); // serializer. text (String. valueOf (person. getAge (); serializer. endTag (null, "age ");//Serializer. endTag (null, "person ");//
   } Serializer. endTag (null, "persons ");//
  Serializer. endDocument (); // end} catch (Exception e) {e. printStackTrace () ;}// use the Pull parser to read the private List of the local XML file
  
   
ParserXmlFromLocal () {try {File path = new File (Environment. getExternalStorageDirectory (), "persons. xml "); FileInputStream FCM = new FileInputStream (path); // get the pull parser object XmlPullParser parser = Xml. newPullParser (); // XmlPullParser parser = XmlPullParserFactory. newInstance (). newPullParser (); // specify the parsed file and encoding format parser. setInput (FCM, "UTF-8"); // obtain the event type int eventType = parser. getEventType (); List
   
    
PersonList = null; Person person = null; String id; while (eventType! = XmlPullParser. END_DOCUMENT) {// obtain the name of the current node String tagName = parser. getName (); switch (eventType) {case XmlPullParser. START_TAG: // The current value is equal to the Start Node.
    
     
If ("persons". equals (tagName )){//
     
      
PersonList = new ArrayList
      
        ();} Else if ("person". equals (tagName )){//
       
         Person = new Person (); id = parser. getAttributeValue (null, "id"); person. setId (Integer. valueOf (id);} else if ("name ". equals (tagName )){//
        
          Person. setName (parser. nextText ();} else if ("age ". equals (tagName) {// person. setAge (Integer. parseInt (parser. nextText ();} break; case XmlPullParser. END_TAG ://
        
       
      
     If ("person ". equals (tagName) {// Add the person object with the set value above to the personList in the set. add (person);} break; default: break;} // obtain the next event type eventType = parser. next ();} return personList;} catch (Exception e) {e. printStackTrace ();} return null ;}}
    
   
  
 

 

 

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.