其他分享
首页 > 其他分享> > 使用XmlSerializer写文件

使用XmlSerializer写文件

作者:互联网

一、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();
    }
}

标签:文件,utf,标签,文档,使用,XmlSerializer,null,serializer
来源: https://blog.csdn.net/a451319296/article/details/105549365