Android pull parses xml files and androidpull parses xml files
In the android system, many resource files are in xml format. In the android system, the xml is parsed using the pul parser, it is the same as sax parsing (I personally think it is simpler than sax) and uses event-driven parsing. When the pull parser starts parsing, we can call its next () method to obtain the next parsing event (that is, the start document, end document, start tag, and end tag). When an element is in, you can call the getAttributte () of XmlPullParser () you can also use its nextText () method to obtain the value of this node.
We want to serialize the object and get the xml file in the following format, named "person. xml:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><persons> <person id="0"> <name>wang0</name> <age>18</age> </person> <person id="1"> <name>wang1</name> <age>19</age> </person> <person id="2"> <name>wang2</name> <age>20</age> </person> <person id="3"> <name>wang3</name> <age>21</age> </person> </persons>
Sometimes, we need to generate an XML file. There are many ways to generate an XML file. For example, we can use only one StringBuilder to group XML content and then write the content into the file; you can also use the pull parser to generate XML files by using DOM APIs. We recommend that you use the Pull parser. We define a Person class to indicate the corresponding content in the xml file:
public class Person {private int id;private String name;private int age;@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";}public Person(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public Person() {super();// TODO Auto-generated constructor stub}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;}}
The following uses the Pull parser to generate an XML file:
Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); writeXmlToLocal () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} private List <Pers On> getPersonList () {List <Person> personList = new ArrayList <Person> (); for (int I = 0; I <4; I ++) {personList. add (new Person (I, "wang" + I, 18 + I);} return personList;}/*** write the xml file locally */private void writeXmlToLocal () {List <Person> personList = getPersonList (); // gets the serialized object XmlSerializer serializer = Xml. newSerializer (); try {// File path = new File (Environment. getExternalStorageDirectory (), "persons. x Ml "); File path = new File ("/data/com. founder. xmldemo/"," persons. xml "); FileOutputStream fos = new FileOutputStream (path); // specify the output location of the serialized object and the encoding serializer. setOutput (fos, "UTF-8"); serializer. startDocument ("UTF-8", true); // Write Start <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> Serializer. startTag (null, "persons"); // <persons> for (Person person: personList) {// start writer serializer. startTag (null, "person"); // <person> serializer. attribute (null, "id", String. valueOf (person. getId (); // write the name serializer. startTag (null, "name"); // <name> serializer. text (person. getName (); serializer. endTag (null, "name"); // </name> // write age serializer. 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 ();}}}
Run the program so that we can find the stored xml file in the/data/com. founder. xmldemo/directory.
Next we can use pull to parse the generated XML file and write the following code:
Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); List <Person> personList = parserXmlFromLocal (); for (Person person: personList) {Log. I ("TestCase", person. toString () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the ac Tion bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;}/*** parses the xml file pull in the SD card * @ return */private List <Person> parserXmlFromLocal () {try {File path = new File (Environment. getExternalStorageDirectory (), "persons. xml "); FileInputStream FCM = new FileInputStream (path); // get the pull parser object XmlPullParser parser = Xml. newPullParser (); // specify the parsed file and encoding format parser. setInput (FCM, "UTF-8"); I Nt eventType = parser. getEventType (); // obtain the event type List <Person> personList = null; Person person = null; String id; while (eventType! = XmlPullParser. END_DOCUMENT) {String tagName = parser. getName (); // get the name of the current node 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;} eventType = parser. next (); // get the next event type} return personList;} catch (Exception e) {e. printStackTrace ();} return null ;}}
Through the above method, we can parse the xml file.
Is it easy?