• 在Android中把内容写到XML文件中


    在Android中把内容写到XML文件中

            saveXmlButton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    List<StudentInfo> studentInfos = StudentInfo.initStudentInfos();
                    try {
                        FileOutputStream os = openFileOutput(fileName, MODE_PRIVATE);
                        //获取XmlSerializer对象
                        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                        org.xmlpull.v1.XmlSerializer xmlSerializer = factory.newSerializer();
                        //设置输出流对象
                        xmlSerializer.setOutput(os, "utf-8");
                        
                        /* 
                         * startDocument(String encoding, Boolean standalone)encoding代表编码方式 
                         * standalone  用来表示该文件是否呼叫其它外部的文件。 
                         * 若值是 ”true” 表示没有呼叫外部规则文件,若值是 ”false” 则表示有呼叫外部规则文件。默认值是 “yes”。 
                         */  
                        xmlSerializer.startDocument("utf-8", true);
                        xmlSerializer.startTag("myNameSpace", "Students");
    
                        
                        for (StudentInfo studentInfo : studentInfos) {
                            xmlSerializer.startTag(null, "student");
                            xmlSerializer.attribute(null, "id", studentInfo.getId()+"");
                            xmlSerializer.startTag(null, "name");
                            xmlSerializer.text(studentInfo.getName());
                            xmlSerializer.endTag(null, "name");
                            
                            xmlSerializer.startTag(null, "address");
                            xmlSerializer.text(studentInfo.getAddress());
                            xmlSerializer.endTag(null, "address");
                            
                            xmlSerializer.startTag(null, "phone");
                            xmlSerializer.text(studentInfo.getPhone());
                            xmlSerializer.endTag(null, "phone");
                            
                            xmlSerializer.endTag(null, "student");
                        }
                        xmlSerializer.endTag("myNameSpace", "Students");
                        xmlSerializer.endDocument();
                        os.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
  • 相关阅读:
    JQ用法
    js查漏补缺【未完】
    VSCode里面HTML添加CSS时没有提示
    CSS查漏补缺【未完】
    HTML查漏补缺 【未完】
    Android Bitmap 全面解析(二)加载多张图片的缓存处理
    android Paint 详解
    Android Bitmap 全面解析(一)加载大尺寸图片
    图片处理框架
    [项目总结]论Android Adapter notifyDataSetChanged与notifyDataSetInvalidated无效原因
  • 原文地址:https://www.cnblogs.com/Rising/p/3305885.html
Copyright © 2020-2023  润新知