Android uses the pull parser to generate XML files and read XML files. androidpull

Source: Internet
Author: User

Android uses the pull parser to generate XML files and read XML files. androidpull

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: <? Xml version = "1.0" encoding = "UTF-8"?> <Persons> <person id = "18"> <name> allen </name> <age> 36 </age> </person> <person id = "28"> <name> james </name> <age> 25 </age> </person> </persons> // use the Pull parser to generate an XML file, write the local private void writeXmlToLocal () {List <Person> personList = getPersonList (); // obtain the serialized object XmlSerializer serializer = Xml. newSerializer (); // XmlSerializer serializer = XmlPullParserFactory. newInstance (). newSerializer (); try {File path = new File (Environm Ent. 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 <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> Serializer. startDocument ("UTF-8", true); serializer. startTag (null, "persons"); // <persons> for (Person person: personList) {// start to write personserializer. startTag (null, "person"); // <person> serializer. attribute (null, "id", String. valueOf (person. getId (); // write the nameserializer of person. startTag (null, "name"); // <name> serializer. text (person. getName (); serializer. endTag (null, "name"); // </name> // write the ageserial of person Izer. startTag (null, "age"); // <age> serializer. text (String. valueOf (person. getAge (); serializer. endTag (null, "age"); // </age> serializer. endTag (null, "person"); // </person>} serializer. endTag (null, "persons"); // </persons> serializer. endDocument (); // end} catch (Exception e) {e. printStackTrace () ;}}// use the Pull parser to read the private List of the local XML File <Person> parserXmlFromLocal () {try {File path = new File (Environment. get ExternalStorageDirectory (), "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 <Person> 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 <person> if ("persons ". equals (tagName) {// <persons> personList = new ArrayList <Person> ();} else if ("person ". equals (tagName) {// <person id = "1"> person = new Person (); id = parser. getAttributeValue (null, "id"); person. setId (Integer. valueOf (id);} else if ("name ". equals (tagName) {// <name> person. setName (parser. nextText ();} else if ("age ". equals (tagName) {// <age> person. setAge (Integer. parseInt (parser. nextText ();} break; case XmlPullParser. END_TAG: // </persons> 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 ;}}

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.