Read and write XML files

Source: Internet
Author: User

XML file: Assets/person.xml

<?XML version= "1.0" encoding= "Utf-8"?><Persons>    < PersonID= "1">        <name>Jack</name>        <Password>12345</Password>    </ Person>    < PersonID= "2">        <name>Tom</name>        <Password>45733</Password>    </ Person>    < PersonID= "3">        <name>Helen</name>        <Password>123s432</Password>    </ Person></Persons>
View Code

Person class:

 PackageCom.example.xmldemo.domain; Public classPerson {Private intID; PrivateString name; PrivateString password;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicPerson (intID, string name, string password) {        Super();  This. ID =ID;  This. Name =name;  This. Password =password; }     PublicPerson (string name, string password) {Super();  This. Name =name;  This. Password =password; }     PublicPerson () {}}
View Code

Convert the contents of the XML file to the object list:

PrivateList<person>xmltoobjlist () {xmlpullparser parser=Xml.newpullparser (); InputStream is=NULL; List<Person> Xmldatas =NewArraylist<person>(); Try{ is= Getassets (). Open ("Person.xml"); //associating the XML document to be parsedParser.setinput (IS, "Utf-8"); Person Person=NULL; intevent =Parser.geteventtype (); //does not equal the document End event loop             while(Event! =xmlpullparser.end_document) {                if(Xmlpullparser.start_tag = =event) {                    //The person tag starts                    if("Person". Equals (Parser.getname ())) { person=NewPerson (); intPID = Integer.parseint (Parser.getattributevalue (0));                    Person.setid (PID); }                    //Name label Start                    if("Name". Equals (Parser.getname ())) {String name=Parser.nexttext ();                    Person.setname (name); }                    if("Password". Equals (Parser.getname ())) {String password=Parser.nexttext ();                    Person.setpassword (password); }                }                if(Xmlpullparser.end_tag = =event) {                    //end of person tag                    if("Person". Equals (Parser.getname ()))                    {Xmldatas.add (person); }} Event=Parser.next (); }            //Verification:             for(person P:xmldatas) {log.i ("XML", p.getname () +P.getpassword ()); }        } Catch(IOException e) {e.printstacktrace (); } Catch(xmlpullparserexception e) {e.printstacktrace (); } finally {            if(Is! =NULL) {                Try{is.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }        returnXmldatas; }

Writes the object list content to xml:

Private voidObjtoxml (list<person>Xmldatas) {XmlSerializer Serializer=Xml.newserializer (); OutputStream OS=NULL; Try {            //Save LocationOS = Openfileoutput ("Person.xml", mode_private); Serializer.setoutput (OS,"Utf-8"); //generating an XML fileSerializer.startdocument ("Utf-8",false); //start tag:<persons>Serializer.starttag (NULL, "persons");  for(person Person:xmldatas) {//<person>Serializer.starttag (NULL, "Person"); //Generate ID PropertySerializer.attribute (NULL, "id", String.valueof (Person.getid ())); //<name> TagsSerializer.starttag (NULL, "name"); //contentSerializer.text (Person.getname ()); //</name> TagsSerializer.endtag (NULL, "name"); Serializer.starttag (NULL, "Password");                Serializer.text (Person.getpassword ()); Serializer.endtag (NULL, "Password"); Serializer.endtag (NULL, "Person"); } Serializer.endtag (NULL, "persons"); //Endserializer.enddocument (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(IllegalStateException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {            if(OS! =NULL) {                Try{os.close (); } Catch(IOException e) {e.printstacktrace (); }}} toast.maketext ( This, "Success", Toast.length_short). Show (); }

Source Address:

Https://github.com/amorypepelu/XMLDemo

Read and write XML files

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.