android 用Pull 创建XML

本文详细介绍了如何使用XML格式保存和读取数据的方法,包括XML序列化、输出设置及数据封装流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

保存代码:
/**
	 * 保存数据到XML文件中
	 * @param persons
	 * @param out
	 * @throws Exception
	 */
	public void save(List<person> persons,OutputStream out) throws Exception{
    XmlSerializer ser=Xml.newSerializer();
    ser.setOutput(out, "UTF-8");
    ser.startDocument("UTF-8",true);
    //主信息
    ser.startTag(null, "persons");
    for(person item: persons)
    {
        //明细信息
        ser.startTag(null, "person");
        
        ser.attribute(null,"id",String.valueOf(item.getId()));
	        //Name
        	ser.startTag(null, "name");
	        	ser.text(item.getName());
	        ser.endTag(null, "name");
	        //age
	        ser.startTag(null, "age");
	        	ser.text(item.getName());
	        ser.endTag(null, "age");
        ser.endTag(null, "person");
    }
    ser.endTag(null, "persons");
	ser.endDocument();
	out.flush();
	out.close();
	}
调用方法:
/**
	 * 信息写入XML中
	 * @param p
	 * @throws Exception
	 */
	private void outXml() throws Exception{
		personservice p=new personservice();
List<person> pl=new ArrayList<person>();
pl.add(new person(43,"zhangxx",80));
pl.add(new person(12,"lili",20));
pl.add(new person(78,"xiaoxiao",8));
File xmlFile=new File(this.getFilesDir(),"itcast.xml");
FileOutputStream out=new FileOutputStream(xmlFile);
p.save(pl, out);
}
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值