Android 常用的XML生成方法

read
1
Android 常用的XML生成方法
PullBuildXMLService.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();
	}
}


person.
package com.android.antking.xml;

public class Person {
	 public int id;
	 public String name;
	 public int age;
	}


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值