如何在android中创建和写入xml文件

时间:2011-06-07 06:07:30

标签: android

我是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

String FILENAME =“hello_file”;                 String string =“hello world!”;

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

2 个答案:

答案 0 :(得分:3)

请勿使用该存储位置。

查看Android and data storage space?

答案 1 :(得分:0)

我建议您查看SharedPreferences(给定页面上的第一个链接)。 如果我没有完全误会,您可以选择存储数据的位置。另请注意实际写入的SharedPreferences.Editor(尤其是apply()函数而不是commit(),因为previous在您的单独线程中处理实际文件访问,这样可以安全地调用来自主线程。)