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

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

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

请勿使用该存储位置。

看看Android和数据存储空间?


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

链接地址: http://www.djcxy.com/p/90007.html

上一篇: How to create and write to xml file in android

下一篇: null pointer exception during android listview refresh