Android Pull parsing xml file

Source: Internet
Author: User

Package Com.example.pullxml;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.util.arraylist;import java.util.List; Import Org.xmlpull.v1.xmlpullparser;import Org.xmlpull.v1.xmlpullparserexception;import Org.xmlpull.v1.xmlserializer;import Android.os.environment;import Android.test.androidtestcase;import Android.util.log;import Android.util.xml;public class Pullxml extends androidtestcase{public void Test () {WriteXML (); list<person> personlist = Parserxml (); for (person person:personlist) {LOG.I ("id", string.valueof (Person.getid () )); LOG.I ("Name", Person.getname ()); LOG.I ("Age", String.valueof (Person.getage ()));}} public void WriteXML () {//Gets the serialized object XmlSerializer serializer = Xml.newserializer (); list<person> personlist = Getpersonlist (); try {File sddir = Environment.getexternalstoragedirectory (); File File = new file (Sddir, "persons.xml"); FileOutputStream fos = new FileOutputStream (file);//Specify Output location and encodingSerializer.setoutput (FOS, "utf-8"); Serializer.startdocument ("Utf-8", true);//<?xml version= ' 1.0 '//encoding= ' Utf-8 '//standalone= ' true '? >serializer.starttag (NULL, "persons");//<persons>for (person person:personlist {Serializer.starttag (null, "person"); Serializer.attribute (NULL, "id", Person.getid (). toString ()); Serializer.starttag (NULL, "name"); Serializer.text (Person.getname ()); Serializer.endtag (null, "name"); Serializer.starttag (NULL, "age"); Serializer.text (String.valueof (Person.getage ())); Serializer.endtag (null, ' age ') ; Serializer.endtag (null, "person");} Serializer.endtag (NULL, "persons");//</persons>serializer.enddocument ();} catch (Exception e) {e.printstacktrace ();}} Public list<person> parserxml () {list<person> personlist = null; person person = null; Long id = null;//Get Pull Parse object Xmlpullparser parser = Xml.newpullparser (); try {File sddir = Environment.getexternalstoragedi Rectory (); File File = new file (Sddir, "persons.xml"); FileInputStream fis = new FIleinputstream (file);p arser.setinput (FIS, "utf-8");//Get event type int eventtype = Parser.geteventtype (); while (eventtype! = xmlpullparser.end_document) {String tagName = Parser.getname ();//get current node name switch (eventtype) {case Xmlpullparser.start _tag:if ("Persons". Equals (TagName)) {personlist = new arraylist<person> ();} else if ("person". Equals (TagName)) { person = new Person (), id = long.parselong (parser.getattributevalue (NULL, "id"));p Erson.setid (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)) {Personlist.add (person);} break;} Eventtype=parser.next ();}} catch (Exception e) {e.printstacktrace (); return null;} return personlist;}  Public list<person> getpersonlist () {list<person> personlist = new arraylist<person> (); for (int i = 0; i < 10; i++) {Person person = new person ((long) I, "Liang" + I, i);p ErsonliSt.add (person);} return personlist;}} Class Person {private Long id;private String name;private Integer age;public person (Long ID, String name, Integer age) {su Per (); this.id = Id;this.name = Name;this.age = age;} Public person () {super ();} @Overridepublic String toString () {return "person [id=" + ID + ", name=" + name + ", age=" + Age + "]";} Public Long GetId () {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Integer Getage () {return age;} public void Setage (Integer age) {this.age = age;}}

Android Pull parsing xml file

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.