Write a simple XML file in the SD card using XmlSerializer

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Write a simple XML file in the SD card using XmlSerializer

Postby noya » Fri Oct 09, 2009 10:47 am

This java file show how to create a new xml file in the device's SD card.

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /************************************************************************************
  2.  * XmlFileCreator.java
  3.  * Author: Francesco Noya (francesco.noya AT studenti.unimi.it)
  4.  *
  5.  * This activity shows how to write an xml file into the SD card using
  6.  * org.xmlpull.v1.XmlSerializer
  7.  *
  8.  * For more informations check these links:
  9.  *
  10.  * http://developer.android.com/reference/ ... lizer.html
  11.  *
  12.  * http://www.ibm.com/developerworks/opens ... tml#list11
  13.  *
  14.  *
  15.  ************************************************************************************/
  16.  
  17.  
  18. package it.noya.test;
  19.  
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24.  
  25. import org.xmlpull.v1.XmlSerializer;
  26.  
  27. import android.app.Activity;
  28. import android.os.Bundle;
  29. import android.os.Environment;
  30. import android.util.Log;
  31. import android.util.Xml;
  32. import android.widget.TextView;
  33.  
  34. public class XmlFileCreator extends Activity {
  35.     @Override
  36.     public void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.main);
  39.         //create a new file called "new.xml" in the SD card
  40.         File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml");
  41.         try{
  42.                 newxmlfile.createNewFile();
  43.         }catch(IOException e){
  44.                 Log.e("IOException", "exception in createNewFile() method");
  45.         }
  46.         //we have to bind the new file with a FileOutputStream
  47.         FileOutputStream fileos = null;        
  48.         try{
  49.                 fileos = new FileOutputStream(newxmlfile);
  50.         }catch(FileNotFoundException e){
  51.                 Log.e("FileNotFoundException", "can't create FileOutputStream");
  52.         }
  53.         //we create a XmlSerializer in order to write xml data
  54.         XmlSerializer serializer = Xml.newSerializer();
  55.         try {
  56.                 //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
  57.                         serializer.setOutput(fileos, "UTF-8");
  58.                         //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
  59.                         serializer.startDocument(null, Boolean.valueOf(true));
  60.                         //set indentation option
  61.                         serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
  62.                         //start a tag called "root"
  63.                         serializer.startTag(null, "root");
  64.                         //i indent code just to have a view similar to xml-tree
  65.                                 serializer.startTag(null, "child1");
  66.                                 serializer.endTag(null, "child1");
  67.                                
  68.                                 serializer.startTag(null, "child2");
  69.                                 //set an attribute called "attribute" with a "value" for <child2>
  70.                                 serializer.attribute(null, "attribute", "value");
  71.                                 serializer.endTag(null, "child2");
  72.                        
  73.                                 serializer.startTag(null, "child3");
  74.                                 //write some text inside <child3>
  75.                                 serializer.text("some text inside child3");
  76.                                 serializer.endTag(null, "child3");
  77.                                
  78.                         serializer.endTag(null, "root");
  79.                         serializer.endDocument();
  80.                         //write xml data into the FileOutputStream
  81.                         serializer.flush();
  82.                         //finally we close the file stream
  83.                         fileos.close();
  84.                        
  85.                 TextView tv = (TextView)this.findViewById(R.id.result);
  86.                         tv.setText("file has been created on SD card");
  87.                 } catch (Exception e) {
  88.                         Log.e("Exception","error occurred while creating xml file");
  89.                 }
  90.     }
  91. }
  92.  
Parsed in 0.487 seconds, using GeSHi 1.0.8.4


Result file should be this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
  2. <root>
  3.   <child1 />
  4.   <child2 attribute="value" />
  5.   <child3>some text inside child3</child3>
  6. </root>
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
User avatar
noya
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Top

Postby padde » Fri Oct 09, 2009 11:13 am

Nice.. thanks for that. That will come in handy for some future
projects i have in mind :)
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby songotho » Fri Nov 06, 2009 3:04 am

Thanks. Nice tutorials. :D
songotho
Developer
Developer
 
Posts: 46
Joined: Tue Mar 03, 2009 1:59 am

Postby base2coder » Sat Nov 07, 2009 7:07 am

Hi...thanks for the nice tutorial. But i am trying to create a new xml in my sd card...can you help me that how can i do that?? Thanks a lot for the tutorial...
base2coder
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sun Jul 12, 2009 8:54 pm

Postby songotho » Tue Nov 10, 2009 8:07 am

base2coder wrote:Hi...thanks for the nice tutorial. But i am trying to create a new xml in my sd card...can you help me that how can i do that?? Thanks a lot for the tutorial...


Hi,

You can see AndroiGPSBlog
Maybe it helps you to create file in sdcard
songotho
Developer
Developer
 
Posts: 46
Joined: Tue Mar 03, 2009 1:59 am

Postby base2coder » Thu Nov 12, 2009 7:00 am

Thanks a lot for the link....
base2coder
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sun Jul 12, 2009 8:54 pm

Top

Postby mikethelearner » Fri Feb 26, 2010 2:23 pm

Hi All,

In Java "XMLEncoder" functionality is to write entire data into a object format.

In Java "XMLDecoder" functionality it to read data in a object format.


But, In Android XMLEncoder and XMLDecoder functionality not supported. Is there any alternative for that XMLEncoder and XMLDecoder classes in Android.

Here I used XMLSerializer and XMLPullParser functions for that construction XML and parsing XML. But these Interfaces supports for to write/read the data i.e hard-coded only. I want to write/read data in a Object format.


Please help me. I need Urgent.


Thanks,
mikethelearner.
mikethelearner
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Feb 15, 2010 3:16 pm
Location: us

Postby spring » Fri Mar 05, 2010 8:22 am

good tutorial,thanks for you sharing 8)
spring
Junior Developer
Junior Developer
 
Posts: 14
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests