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"); FileOutputStream outStream = new FileOutputStream(xmlFile); OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8"); BufferedWriter writer = new BufferedWriter(outStreamWriter); writeXML(persons, writer); writer.flush(); writer.close(); 如果只想得到生成的xml字符串内容,可以使用StringWriter: StringWriter writer = new StringWriter(); writeXML(persons, writer); String content = writer.toString(); */

 

<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>zhangsan</name> <age>25</age> </person> <person id="20"> <name>lisi</name> <age>28</age> </person> </person

 

package com.zyq.voo; public class Person { private Integer id; private String name; private Short age; public Person() { } public Person(Integer id, String name, Short age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }

 

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zyq.xml" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.zyq.xml" android:label="Tests for My App" /> </manifest>

 

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); } }

 

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(); } }

 

Android 采用pull生成XML数据_第1张图片

你可能感兴趣的:(Android 采用pull生成XML数据)