Android XML file read/write

Source: Internet
Author: User
<? XML version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> eboy </Name>
<Age> 22 </age>
</Person>
<Person id = "24">
<Name> FRR </Name>
<Age> 20 </age>
</Person>
</Persons>

Package com. eboy. Service;

Import java. Io. inputstream;
Import java. Io. outputstream;
Import java. util. arraylist;
Import java. util. List;
Import org. xmlpull. v1.xmlpullparser;
Import org. xmlpull. v1.xmlserializer;

Import com. eboy. domain. person;

Import Android. util. xml;

Public class personservice {
Public static list <person> getpersons (inputstream XML) throws exception {
List <person> Persons = NULL;
Person = NULL;
Xmlpullparser pullparser = xml. newpullparser ();
Pullparser. setinput (XML, "UTF-8"); // set the XML data to be parsed for the pull Interpreter
Int event = pullparser. geteventtype ();

While (event! = Xmlpullparser. end_document ){

Switch (event ){

Case xmlpullparser. start_document:
Persons = new arraylist <person> ();
Break;
Case xmlpullparser. start_tag:
If ("person". Equals (pullparser. getname ())){
Int id = integer. valueof (pullparser. getattributevalue (0 ));
Person = new person ();
Person. setid (ID );
}
If ("name". Equals (pullparser. getname ())){
String name = pullparser. nexttext ();
Person. setname (name );
}
If ("Age". Equals (pullparser. getname ())){
Int age = integer. valueof (pullparser. nexttext ());
Person. setage (AGE );
}
Break;

Case xmlpullparser. end_tag:
If ("person". Equals (pullparser. getname ())){
Persons. Add (person );
Person = NULL;
}
Break;

}

Event = pullparser. Next ();
}


Return persons;
}

/**
* Save the data to an XML file.
* @ Param persons
* @ Param out
* @ Throws exception
*/
Public static void save (list <person> persons, outputstream out) throws exception {
Xmlserializer serializer = xml. newserializer ();
Serializer. setoutput (Out, "UTF-8 ");
Serializer. startdocument ("UTF-8", true );
Serializer. starttag (null, "persons ");
For (person: Persons ){
Serializer. starttag (null, "person ");
Serializer. Attribute (null, "ID", person. GETID (). tostring ());
Serializer. starttag (null, "name ");
Serializer. Text (person. getname (). tostring ());
Serializer. endtag (null, "name ");
Serializer. starttag (null, "Age ");
Serializer. Text (person. getage (). tostring ());
Serializer. endtag (null, "Age ");
Serializer. endtag (null, "person ");
}
Serializer. endtag (null, "persons ");
Serializer. enddocument ();
Out. Flush ();
Out. Close ();
}
}

/Files/jxgxy/com.eboy.readwritexml.mainactivity.rar

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.