zoukankan      html  css  js  c++  java
  • Android生成xml

    XmlSerializer

        //使用XmlSerializer来序列化xml文件
        public static boolean backupSms_android(Context context){
            
            try{
                
                //0.获取短信数据
                ArrayList<SmsBean> allSms = SmsDao.getAllSms();
                //1.通过Xml获取一个XmlSerializer对象
                XmlSerializer xs = Xml.newSerializer();
                //2.设置XmlSerializer的一些参数,比如:设置xml写入到哪个文件中
                //os:xml文件写入流   encoding:流的编码
                xs.setOutput(context.openFileOutput("backupsms2.xml", Context.MODE_PRIVATE), "utf-8");
                //3.序列化一个xml的声明头
                //encoding:xml文件的编码  standalone:是否独立
                xs.startDocument("utf-8", true);
                //4.序列化一个根节点的开始节点
                //namespace:命名空间  name: 标签的名称
                xs.startTag(null, "Smss");
                //5.循环遍历list集合序列化一条条短信
                
                    for (SmsBean smsBean : allSms) {
                        xs.startTag(null, "Sms");
                        //name:属性的名称  value:属性值
                        xs.attribute(null, "id", smsBean.id+"");
                        
                        xs.startTag(null, "num");
                        //写一个标签的内容
                        xs.text(smsBean.num);
                        xs.endTag(null, "num");
                        
                        
                        xs.startTag(null, "msg");
                        xs.text(smsBean.msg);
                        xs.endTag(null, "msg");
                        
                        
                        xs.startTag(null, "date");
                        xs.text(smsBean.date);
                        xs.endTag(null, "date");
                        
                        xs.endTag(null, "Sms");
                    }

                //6.序列化一个根节点的结束节点
                    xs.endTag(null, "Smss");
                //7.将xml写入到文件中,完成xml的序列化
                    xs.endDocument();
                    return true;

            }catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

  • 相关阅读:
    C# 规格说明书
    C#学习笔记----复习笔记
    C#学习笔记-stream,win8.1开发小记
    C#学习笔记-Win8.1 store app winform开发小记
    C#学习笔记:linq和xml
    C#第六周--关于正则表达式应用,delegates关键字
    C#第六课---struct,interface
    C#第五课--继承和多态
    开发了一款小程序
    「CSS Warning 2」icon 的做法
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/5664889.html
Copyright © 2011-2022 走看看