本文实例讲述了AndroID中常用的XML生成方法。分享给大家供大家参考。具体如下:
1. java代码:
package com.androID.antking.xml;import java.io.OutputStream;import java.util.List;import org.xmlpull.v1.XmlSerializer;import androID.util.Xml;/**采用pull 生成xml文件 * * @author antkingwei * */public class PullBuildXMLService { 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,"perisons"); for(Person person:persons){ serializer.startTag(null,"perison"); serializer.attribute(null,"ID",String.valueOf(person.ID)); serializer.startTag(null,"name"); serializer.text(person.name); serializer.endTag(null,"name"); serializer.startTag(null,"age"); serializer.text(String.valueOf(person.age)); serializer.endTag(null,"age"); serializer.endTag(null,"perison"); } serializer.endTag(null,"perisons"); serializer.enddocument(); outputStream.close(); }}
2. java ben:
package com.androID.antking.xml;public class Person { public int ID; public String name; public int age;}
3. 调用方法:
public voID writefile() throws Throwable{ List<Person> List = new ArrayList<Person>(); for(int i =0;i<10;i++){ Person person = new Person(); person.ID = 1; person.name = "ant"; person.age = 12; List.add(person); } file file = new file(MainActivity.this.getfilesDir(),"person.xml"); fileOutputStream outputStream = new fileOutputStream(file); PullBuildXMLService service = new PullBuildXMLService(); service.buildXML(List,outputStream);}
希望本文所述对大家的AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android中常用的XML生成方法实例分析全部内容,希望文章能够帮你解决Android中常用的XML生成方法实例分析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)