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.app.Activity;
import android.os.Bundle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        }catch(FileNotFoundException e)
        {
            Log.e("FileNotFoundException",e.toString());
        }
        XmlSerializer serializer = Xml.newSerializer();
        try{
        serializer.setOutput(fileos, "UTF-8");
        serializer.startDocument(null, Boolean.valueOf(true));
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", 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");
        serializer.endTag(null,"Child3");
        serializer.endTag(null,"root");
        serializer.endDocument();
        serializer.flush();
        fileos.close();
        //TextView tv = (TextView)findViewById(R.);

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

  • is it possible to create listview inside dialog?
  • How to check certificate name and alias in keystore files?
  • Android Browser stretches image
  • Disable collapsing of ExpandableListView
  • react-native-tcp not working on Android 7+
  • React native textDecoration properties not working on Android
  • Related posts:

    Setting webview background image to a resource graphic in Android
    Find out if Android device is portrait or landscape for normal usage?
    Android Studio 2.0 launch APK on another device
    Android BLE, read and write characteristics
    How to create an excel file in android?
    Android: access SQLite database via Content Provider or implement DAO?
  • Making ArrayList of custom objects parcelable
  • Update data in ListFragment as part of ViewPager
  • Android how do I wait until a service is actually connected?
  • Returning from an activity using navigateUpFromSameTask()
  • Widget disappearing every time the app is updated on Samsung devices
  • How can a divider line be added in an Android RecyclerView?
  • 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.

    http://developer.android.com/guide/topics/data/data-storage.html 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);
    fos.write(...);
    

    That should put it relative to your app.

    Have you added the necessary permission for this?

    http://developer.android.com/reference/android/Manifest.permission.html

    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.