一、XmlSerializer
XmlSerializer serializer= Xml.newSerializer();
serializer.setOutput(fos,"utf-8");
//开始文档(字符集,文档是否独立)
serializer.startDocument("utf-8",true);
//开始一个标签(参数:命名空间,标签名)
serializer.startTag(null,"name");
//结束一个标签(参数:命名空间,标签名)
serializer.endTag(null,"name");
//结束文档
serializer.endDocument();
二、代码示例
public void XmlSave(View view) { XmlSerializer serializer= Xml.newSerializer(); File file=new File(this.getFilesDir(),"book.xml"); try { OutputStream out=new FileOutputStream(file); serializer.setOutput(out,"utf-8"); //开始文档(字符集,文档是否独立) serializer.startDocument("utf-8",true); serializer.startTag(null,"Books"); for (int i=0;i<10;i++) { String name="西游记"+i; String price="吴承恩"; serializer.startTag(null,"book"); serializer.startTag(null,"name"); serializer.text(name); serializer.endTag(null,"name"); serializer.startTag(null,"price"); serializer.text(price); serializer.endTag(null,"price"); serializer.endTag(null,"book"); } serializer.endTag(null,"Books"); serializer.endDocument(); } catch (Exception e) { e.printStackTrace(); } }