Android-write XML

Source: Internet
Author: User

On the basis of the personservice, add savepersons (...), then the personservice is:

package com.njupt.xml;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;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.util.Xml;public class PersonService {public List<Person> loadPersons(InputStream in) throws Exception{XmlPullParser parser = Xml.newPullParser();parser.setInput(in,"UTF-8");List<Person> persons = new ArrayList<Person>();Person p = null;for(int type = parser.getEventType() ; type != XmlPullParser.END_DOCUMENT ; type = parser.next()){if(type == XmlPullParser.START_TAG){if(parser.getName().equals("person")){p = new Person();String id = parser.getAttributeValue(0);p.setId(Integer.parseInt(id));persons.add(p);}else if(parser.getName().equals("name")){String name = parser.nextText();p.setName(name);}else if(parser.getName().equals("age")){String age = parser.nextText();p.setAge(Integer.parseInt(age));}}}return persons;}public void savePersons(List<Person> persons,FileOutputStream out) throws Exception {XmlSerializer serializer = Xml.newSerializer();serializer.setOutput(out,"UTF-8");serializer.startDocument("UTF-8", true);serializer.startTag(null, "persons");for(Person p : persons){serializer.startTag(null, "person");serializer.attribute(null, "id", p.getId().toString());serializer.startTag(null, "name");serializer.text(p.getName());serializer.endTag(null, "name");serializer.startTag(null, "age");serializer.text(p.getAge().toString());serializer.endTag(null, "age");serializer.endTag(null, "person");}serializer.endTag(null, "persons");serializer.endDocument();}}

2. persontest

package com.njupt.xml;import java.io.FileOutputStream;import java.io.InputStream;import java.util.List;import android.test.AndroidTestCase;public class PersonTest extends AndroidTestCase{public void test1() throws Exception{PersonService service = new PersonService();InputStream in = PersonTest.class.getClassLoader().getResourceAsStream("persons22.xml");    List<Person> persons = service.loadPersons(in); for(Person p : persons){System.out.println(p);}Person p = new Person(4,"allen",20);persons.add(p);service.savePersons(persons,new FileOutputStream("/mnt/sdcard/persons22.xml"));}}

3. person22.xml

The content of the generated person22.xml file is as follows (note that it will not help us format the XML file ):

<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Persons> <person id = "1"> <Name> Fan Bingbing </Name> <age> 31 </age> </person> <person id = "2"> <name> Lin Zhiling </Name> <age> 38 </age> </person> <person id = "3"> <Name> Yang Mi </Name> <age> 26 </age> </person> <person id = "4"> <Name> Allen </Name> <age> 20 </age> </person> </persons>

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.