---
title: "android 中生成XML文件的代码"
author: "googlelee"
date: "2012-05-17T18:09:08Z"
tags: []
categories: ["移动开发"]
slug: "android-xml"
draft: false
lastmod: "2012-05-17T18:09:08Z"
id: 1533987
url: "https://googlelee.iteye.com/blog/1533987"
---

try {

File f = new File(getExternalCacheDir().getAbsolutePath()+"my.xml");

OutputStream outPut = new FileOutputStream(f);

XmlSerializer serializer=Xml.newSerializer();  

serializer.setOutput(outPut, "utf-8");  

 

serializer.startDocument("utf-8", true);  

serializer.startTag(null, "companys");  

 

for(String\[] s:taxiCompany)  

{  

   serializer.startTag(null, DBUtil.TAXI\_TABLE);  

   

   serializer.attribute(null, DBUtil.KEY\_PROVINCE, s\[0]);  

   serializer.attribute(null, DBUtil.KEY\_CITYNAME, s\[1]);

   serializer.attribute(null, DBUtil.KEY\_NAME, s\[2]);  

   serializer.attribute(null, DBUtil.KEY\_TELE, s\[3]);  

     

   serializer.endTag(null, DBUtil.TAXI\_TABLE);  

}  

 

serializer.endTag(null, "companys");  

serializer.endDocument();  

outPut.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}