码迷,mamicode.com
首页 > 其他好文 > 详细

Xml序列化

时间:2018-12-30 18:52:27      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:生成   class   log   pen   date   end   new   make   stringbu   

  • xml序列化
    • 实现思路 通过程序生成一个xml文件来备份手机短信. 先获取手机短信的内容 —>通过xml备份.
    • StringBuffer 代码如下
    public void click(View view) {
        StringBuffer sb = new StringBuffer();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.append("<SMSS>");
        for (SMSbean list : mLists) {
            sb.append("<SMS>");
            sb.append("<address>");
            sb.append(list.getAddress());
            sb.append("</address>");
            sb.append("<body>");
            sb.append(list.getBody());
            sb.append("</body>");
            sb.append("<date>");
            sb.append(list.getDate());
            sb.append("</date>");
            sb.append("</SMS>");
        }
        sb.append("<SMSS>");
        try {
            String s = sb.toString();
//            Log.d(TAG, "click: "+s);
            String path = Environment.getExternalStorageDirectory().getAbsolutePath();
            Log.d(TAG, "click: " + path);
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Config.xml");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(s.getBytes());
            fos.close();
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }

 

 
    • 通过官方提供序列化类实现xml序列化(通过程序生成一个xml)
    public void click1(View view) {
        try {
            XmlSerializer serializer = Xml.newSerializer();
            File file = new File(getFilesDir().getPath(), "ConfigCopy.xml");
            String path = getFilesDir().getPath();
            Log.d(TAG, "click1: " + path);
            FileOutputStream fos = new FileOutputStream(file);
            serializer.setOutput(fos, "utf-8");
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "smss");
            for (int i = 0; i < mLists.size(); i++) {
                serializer.startTag(null, "sms");
                serializer.startTag(null, "address");
                serializer.text(mLists.get(i).getAddress());
                serializer.endTag(null, "address");
                serializer.startTag(null, "body");
                serializer.text(mLists.get(i).getBody());
                serializer.endTag(null, "body");
                serializer.startTag(null, "date");
                serializer.text(mLists.get(i).getDate());
                serializer.endTag(null, "date");
                serializer.endTag(null, "sms");
            }
            serializer.endTag(null, "smss");
            serializer.endDocument();
            fos.close();
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }

 

 

Xml序列化

标签:生成   class   log   pen   date   end   new   make   stringbu   

原文地址:https://www.cnblogs.com/nangongyibin/p/10200050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!