How to create and write to xml file in android
<p>I'am beginner to android I have an emergency contact screen in that I have two fields like (email,phone number) I want to save these things in <code>xml</code> instead of <code>sqlite</code>. I used following code for saving but I unable to create xml file in internal memory,it's giving exception please see the code below.</p>

<p>also i used this code shown in code-2 below using that i unable to read values and how can i see the file is created or what please help me.</p>

<p>Advance thanks,</p>

<pre><code>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 &lt;?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");
                    }
</code></pre>

<p>code-2</p>

<p>String FILENAME = "hello_file";
                String string = "hello world!";</p>

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