---
title: "Android xml序列化存储"
author: "hellojyj"
date: "2014-10-30T23:41:48Z"
tags: []
categories: ["移动开发"]
slug: "android-xml"
draft: false
lastmod: "2014-10-30T23:41:48Z"
id: 2149942
url: "https://hellojyj.iteye.com/blog/2149942"
---

```
public void saveMessage(View v) {
		try {
			XmlSerializer serializer = Xml.newSerializer();
			File file = new File(Environment.getExternalStorageDirectory(),
					"sms.xml");
			FileOutputStream fos = new FileOutputStream(file);
			serializer.setOutput(fos,"utf-8");
			serializer.startDocument("utf-8", true);
			serializer.startTag(null, "smss");
			
			serializer.startTag(null, "sms");
			serializer.attribute(null, "id", "sms0001");
			
			serializer.startTag(null, "title");
			serializer.text("早上开会");
			serializer.endTag(null, "title");
			
			serializer.startTag(null, "body");
			serializer.text("今天早上，5点，报告厅开会");
			serializer.endTag(null, "body");
			
			serializer.startTag(null, "sender");
			serializer.text("经理");
			serializer.endTag(null, "sender");
			
			serializer.startTag(null, "time");
			serializer.text("2014-10-22 15:30");
			serializer.endTag(null, "time");
			
			serializer.endTag(null, "sms");
			
			serializer.endTag(null, "smss");
			serializer.endDocument();
			
			fos.close();
			Toast.makeText(this, "备份成功", Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "备份失败", Toast.LENGTH_SHORT).show();
		}
		
	}
```