【问题标题】:How to create and write to xml file in android如何在android中创建和写入xml文件
【发布时间】:2011-06-07 06:07:29
【问题描述】:

我是 android 的初学者,我有一个紧急联系人屏幕,其中有两个字段,例如(电子邮件、电话号码)我想将这些内容保存在 xml 而不是 sqlite 中。我使用以下代码进行保存,但无法在内存中创建 xml 文件,出现异常请参阅下面的代码。

我也使用了下面代码 2 中显示的这段代码,我无法读取值以及如何查看文件已创建或请帮助我。

提前谢谢,

TextView txtemailid=(TextView)findViewById(R.id.EmailId);
            TextView txtphoneno=(TextView)findViewById(R.id.phoneNo);

            File newxmlfile = new File("/data/com.itwine/emergency.xml");

            try{
                    newxmlfile.createNewFile();
            }catch(IOException e){
                    Log.e("IOException", "exception in createNewFile() method");
            }
            //we have to bind the new file with a FileOutputStream
            FileOutputStream fileos = null;        
            try{
                    fileos = new FileOutputStream(newxmlfile);
            }catch(FileNotFoundException e){
                    Log.e("FileNotFoundException", "can't create FileOutputStream");
            }
            //we create a XmlSerializer in order to write xml data
            XmlSerializer serializer = Xml.newSerializer();
            try {
                    //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
                            serializer.setOutput(fileos, "UTF-8");
                            //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
                            serializer.startDocument(null, Boolean.valueOf(true));
                            //set indentation option
                            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                            //start a tag called "root"
                            serializer.startTag(null, "root");
                            *//**serializer.startTag(null, "Child1");
                            serializer.endTag(null, "Child1");
                            serializer.startTag(null, "Child2");
                            serializer.attribute(null, "attribute", "value");
                            serializer.endTag(null, "Child2");*//*
                            serializer.startTag(null, "EmailId");
                            serializer.text(txtemailid.getText().toString());
                            serializer.endTag(null,"EmailId");
                            serializer.startTag(null, "PhoneNo");
                            serializer.text(txtphoneno.getText().toString());
                            serializer.endTag(null,"PhoneNo");
                            serializer.endTag(null,"root");
                            serializer.endDocument();
                            //write xml data into the FileOutputStream
                            serializer.flush();
                            //finally we close the file stream
                            fileos.close();
                           Toast.makeText(getApplication(), "xml created",Toast.LENGTH_LONG);
                    } catch (Exception e) {
                            Log.e("Exception","error occurred while creating xml file");
                    }

代码-2

字符串文件名 = "hello_file"; String string = "你好世界!";

            FileOutputStream fos=null;
            try {
                fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fos.write(string.getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

【问题讨论】:

  • 请贴出异常的堆栈跟踪,否则只能猜到你的错误。
  • 标题是“创建并写入 xml”,选择的答案是“不使用 xml”。更改标题或删除帖子?

标签: android


【解决方案1】:

请勿使用该存储位置。

Android and data storage space?

【讨论】:

    【解决方案2】:

    我建议您查看SharedPreferences(给定页面上的第一个链接)。 如果我没有完全弄错,您可以选择存储数据的位置。还要注意用于实际写入的SharedPreferences.Editor(尤其是apply() 函数而不是commit(),因为先前在单独的线程中为您处理实际文件访问,这使得从主线程调用是安全的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-07
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多