The steps to make it are:
- create a File object
- link file object to a FileOutputStream
- create a XmlSerializer object and link to the FileOutputStream
- use XmlSerializer methods to create xml tree
- use XmlSerializer flush() method to write data into the new file
NOTICE: I've tested the application in a real device. NOT in the emulator! In order to work correctly into the Emulator i suppose you've got to set up sdcard emulation correctly (in the forum there's a topic about it)
here's the code:
Using java Syntax Highlighting
- /************************************************************************************
- * XmlFileCreator.java
- * Author: Francesco Noya (francesco.noya AT studenti.unimi.it)
- *
- * This activity shows how to write an xml file into the SD card using
- * org.xmlpull.v1.XmlSerializer
- *
- * For more informations check these links:
- *
- * http://developer.android.com/reference/ ... lizer.html
- *
- * http://www.ibm.com/developerworks/opens ... tml#list11
- *
- *
- ************************************************************************************/
- package it.noya.test;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.xmlpull.v1.XmlSerializer;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.util.Xml;
- import android.widget.TextView;
- public class XmlFileCreator extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.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");
- }
- //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");
- //i indent code just to have a view similar to xml-tree
- serializer.startTag(null, "child1");
- serializer.endTag(null, "child1");
- serializer.startTag(null, "child2");
- //set an attribute called "attribute" with a "value" for <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();
- //write xml data into the FileOutputStream
- serializer.flush();
- //finally we close the file stream
- fileos.close();
- TextView tv = (TextView)this.findViewById(R.id.result);
- tv.setText("file has been created on SD card");
- } catch (Exception e) {
- Log.e("Exception","error occurred while creating xml file");
- }
- }
- }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Result file should be this:







