29_xml文件的序列化

public void backUp2(View view){
		//获得xml序列化对象
		XmlSerializer xs =Xml.newSerializer();
		try {
			File file = new File(Environment.getExternalStorageDirectory(),"backUp2.xml");
			FileOutputStream fs = new FileOutputStream(file);
			//xml文件写入输出流,以utf-8编码保存文件
			xs.setOutput(fs, "utf-8");
			xs.startDocument("utf-8", true);//开始文档,encoding编码格式,是否是独立文件
			xs.startTag(null, "Smss");//开始标签
			xs.attribute(null, "id", "sms");//添加id属性
			for(Sms sms : list){
				xs.startTag(null,"body");
				xs.text(sms.getBody());
				xs.endTag(null, "body");
				
				xs.startTag(null, "address");
				xs.text(sms.getAddress());
				xs.endTag(null, "address");
				
				xs.startTag(null, "date");
				xs.text(Long.toString(sms.getDate()));
				xs.endTag(null, "date");
				
				xs.startTag(null, "type");
				xs.text(Integer.toString(sms.getType()));
				xs.endTag(null, "type");
			}
			xs.endTag(null, "Smss");//结束标签
			xs.endDocument();//结束
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "保存失败!", 0);
		}
	}



XmlSerializer xs =Xml.newSerializer();
面向对象XML

你可能感兴趣的:(29_xml文件的序列化)