how to create xml file in android

Guys i have a problem that my code gives an Exception as Permission Denied when we write a xml in android. can any one tell that How it will be removed.

package com.ex.createXml;

import android.os.Bundle;
import org.xmlpull.v1.XmlSerializer;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;

public class createXml extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        File newxmlfile = new File("/data/new.xml");
        }catch(IOException e)
            Log.e("IOException", "Exception in create new File(");
        FileOutputStream fileos = null;
            fileos = new FileOutputStream(newxmlfile);

        }catch(FileNotFoundException e)
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(fileos, "UTF-8");
        serializer.startDocument(null, Boolean.valueOf(true));
        serializer.setFeature("", true);
        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, "Child3");
        serializer.text("Some text inside child 3");
        //TextView tv = (TextView)findViewById(R.);

        }catch(Exception e)
            Log.e("Exception","Exception occured in wroting");

  • How to install old version of Android build tools from command line?
  • Listview divider margin
  • Best way to periodically executing AsyncTasks in Android
  • Android Widget: Show configuration activity before widget is added to the screen
  • Instrumented tests failure with AndroidJUnitRunner 1.0.0 and AssertJ
  • push notifications on all 3 platforms (android,ios,windows phone)
  • Related posts:

    How can you tell if a View is visible on screen in Android?
    nested scrollview + recyclerview, strange autoscroll behaviour
    Android SeekBar Touch Sensitivity
    Google Analytics Android SDK V4 confusion
    How to give single line border to gridview android
    how to change default navigation tab view to customized tab view?
  • Android:What is difference between setFlags and addFlags for intent
  • Sprite and animation making tool for android game
  • Passing nested Class<MyInterface<T>> as a parameter in Android
  • AppCompatActivity as a dialog without title
  • RSA Encryption: Difference between Java and Android
  • Read all SMS from a particular sender
  • 7 Solutions collect form web for “how to create xml file in android”

    Try this to create or read file in android.
    working with files

    and to create/read XML file try Working with XML

    If you need any demo project related to xml than let me know, i’ll try to give you

    File newxmlfile = new File("C:/new.xml");

    You’re trying to create your file on drive C. Android is a linux-based system, so there are no drives there. The storage devices can be mounted to root (“/”) or any other folder.

    For your application /data/<pakcage-name> folder is available. Try to write there. Also, you can try to write to external storage, but your progrma will need a permission to do that:

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    This should be mentioned within your manifest file.

    Read more here.

    It seems like your trying to create a file at the location of c:/ which is not a valid path identifier on a android system. Android comes from a linux environment. follow this link to learn more about data storage on android

    If you want to create a file on your local pc you first need to create it on your Android device and then pull it from the device (either the emulator or your real phone)

    You’re not allowed to write to that location.

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

    You could save it to the internal storage, or cache.

    Due to the Android security model you can’t write to /data/new.xml. That’s trying to right to the root file system which is why you are getting permission denied. Try it without the leading slash.

    FileOutputStream fos = openFileOutput("new.xml", MODE_PRIVATE);

    That should put it relative to your app.

    Have you added the necessary permission for this?

    Permission to write to the SD card

    You can use this:

    File xmlDir = new File(Environment.getExternalStorageDirectory().getPath() + "/data/new.xml");
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.