1

I tried this code but i don't know where I will find the xml file created

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //create a new file called "new.xml" in the SD card
    File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml");
    try{
        newxmlfile.createNewFile();
    }catch(IOException e){
        Log.e("IOException", "exception in createNewFile() method");
    }
    FileOutputStream fileos = null;         
    try{
        fileos = new FileOutputStream(newxmlfile);
    }catch(FileNotFoundException e){
        Log.e("FileNotFoundException", "can't create FileOutputStream");
    }

    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");
            //write some text inside <child3>
            serializer.text("some text inside child3");
            serializer.endTag(null, "child3");

        serializer.endTag(null, "root");
        serializer.endDocument();
        serializer.flush();
        fileos.close();

           TextView tv = (TextView)this.findViewById(R.id.result);
           tv.setText("file has been created on SD card");
           System.out.println("file created");
    } catch (Exception e) {
        Log.e("Exception","error occurred while creating xml file");
    }
   }
   }
1

2 Answers 2

0

It differs with the model of the phone, and also if you are having any external storage. Best way to check the path of file is to do it in your app. You should use:

String path = newxmlfile.getAbsolutePath();
0

the file must be created in the sdcard if you don't find it then there is a problem in the code

1
  • i found it finally :) thkx for all
    – Marya
    Commented Mar 4, 2014 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.