Read and Write Android xml files in Android

Source: Internet
Author: User
Android XML file read/write

 

<? 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 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 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

Reprinted address

Http://www.cnblogs.com/jxgxy/archive/2012/07/26/2609730.html

 

<? 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 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 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

Reprinted address

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.