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

更多相关文章

  1. 【Android】volley网络框架的文件下载
  2. android 对文件的操作模式
  3. Android从网上下载文件
  4. android实现Parcelable序列化对象
  5. Android Intent传递对象和集合
  6. Android遍历某个文件夹的图片并实现滑动查看的的Gallery
  7. Android SDcard目录文件操作
  8. android sdcard文件存储 + 媒体库更新方法

随机推荐

  1. Android(安卓)学习路线总结
  2. android dialog 不变暗
  3. Android Desigin Library
  4. android设置属性
  5. 【30篇突击 android】源码统计七
  6. BlueStacks将Android程序运行在Windows上
  7. android 逐帧动画
  8. Android开发学习之Gallery和GridView浅析
  9. 网络请求框架AsyncHttpclient的简单使用
  10. RadioGroup+RadioButton嵌套实现多行单选