(转)Android 采用pull生成XML数据
些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中;或者使用DOM API生成XML文件,或者也可以使用pull解析器生成XML文件,这里推荐大家使用Pull解析器。?
使用Pull解析器生成一个与itcast.xml文件内容相同的myitcast.xml文件,代码在本页下方备注?
使用代码如下(生成XML文件):?File xmlFile = new File("myitcast.xml");
?
?
?
?
?
?
view plaincopy to clipboardprint? package com.zyq.xml; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import com.zyq.service.DOMPersonService; import com.zyq.service.PullBuildXMLService; import com.zyq.service.PullPersonService; import com.zyq.service.SAXPersonService; import com.zyq.voo.Person; import android.test.AndroidTestCase; import android.util.Log; public class PersonServiceTest extends AndroidTestCase { private static final String TAG="PersonServiceTest"; /** * 测试 SAX解析XML数据 * @throws Throwable */ public void testSAX() throws Throwable { SAXPersonService personService=new SAXPersonService(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons=personService.getPersons(inputStream); for(Person person : persons) { Log.i(TAG, person.toString()); } } /** * 测试 DOM解析XML数据 * @throws Throwable */ public void testDOM() throws Throwable { DOMPersonService personService=new DOMPersonService(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons=personService.getPersons(inputStream); for(Person person : persons) { Log.i(TAG, person.toString()); } } /** * 测试 PULL解析XML数据 * @throws Throwable */ public void testPull() throws Throwable { PullPersonService personService=new PullPersonService(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons=personService.getPersons(inputStream); for(Person person : persons) { Log.i(TAG, person.toString()); } } public void testPullBuildXML() throws Throwable { List<Person> persons=new ArrayList<Person>(); persons.add(new Person(1001,"张三",(short)30)); persons.add(new Person(1002,"李四",(short)18)); persons.add(new Person(1003,"王二麻子",(short)21)); persons.add(new Person(1004,"朱大",(short)25)); persons.add(new Person(1005,"林一",(short)20)); File file=new File(this.getContext().getFilesDir(),"zyq.xml"); FileOutputStream outputStream=new FileOutputStream(file); PullBuildXMLService service=new PullBuildXMLService(); service.buildXML(persons, outputStream); } } view plaincopy to clipboardprint? package com.zyq.service; import java.io.OutputStream; import java.util.List; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import com.zyq.voo.Person; /** * 采用PULL 生成XML数据 * @author Administrator * */ public class PullBuildXMLService { /** * * @param persons * @param outputStream * @throws Exception */ public void buildXML(List<Person> persons,OutputStream outputStream) throws Exception { XmlSerializer serializer=Xml.newSerializer(); serializer.setOutput(outputStream, "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()); 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(); outputStream.close(); } }??