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.042 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.001 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
Experienced Developer
Experienced Developer
 
Posts: 55
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: 14
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
Experienced Developer
Experienced Developer
 
Posts: 55
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: 14
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: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Re: Write a simple XML file in the SD card using XmlSerializ

Postby RenatoI » Fri Sep 03, 2010 11:01 am

Thx for example. In my case it is all working fine with small files. But if I want to write xml file larger than 16.00KB, it stops writing to the file. Must I set somekind of max limit for FileOutputStream?
RenatoI
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Sep 03, 2010 10:57 am

Re: Write a simple XML file in the SD card using XmlSerializ

Postby devashish » Fri Feb 11, 2011 7:34 pm

File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml"); throws an io exception
instead use
File newxmlfile = new File( Environment.getExternalStorageDirectory(),"new.xml");
devashish
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Feb 11, 2011 7:32 pm

Re: Write a simple XML file in the SD card using XmlSerializ

Postby Mizzhell » Tue Apr 12, 2011 12:42 pm

I want to continue to add tags after the child3-tag. E.g. I want the app to save playernames and who won. So after every game I want to write to the xml-file. But how do I continue after the last tag within the root?
Mizzhell
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 12, 2011 12:21 pm

Re: Write a simple XML file in the SD card using XmlSerializ

Postby jodroid » Wed Jun 15, 2011 7:17 pm

mizzhell,

What is the app you are trying to develop? It sounds like you are interested in sports which is the direction I am headed as well. I am trying to find some of the bigger high school baseball markets and keep track of stats, records, recruiting etc. all for a single app. Are you working on a similar project or something on a more professional level? I have been looking at online reviews trying to pick up an assistant, have you ever checked out IT staffing agencies for android developer job reviews?
Last edited by jodroid on Fri Jan 20, 2012 10:56 pm, edited 1 time in total.
jodroid
Freshman
Freshman
 
Posts: 5
Joined: Tue Jun 14, 2011 8:09 pm

Re: Write a simple XML file in the SD card using XmlSerializ

Postby mazen » Tue Sep 06, 2011 12:31 pm

Hi all,
since you save my time with your tutorial ..
the least thing I can do to register in this forum and say
thank you
....
mazen
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Sep 06, 2011 12:25 pm

Re: Write a simple XML file in the SD card using XmlSerializ

Postby Krodil » Mon Oct 31, 2011 7:48 pm

Hey, thanks for sharing this :)
I have one novice question: How do you write deeper levels. If I want root>child>child>etc.

In my case i need a root, which will be a user name <NAME> (by key input). Then some children in the same level <experiment1>, <exp2...> etc.
In each of those experiments I need some underlying levels.
|
|root <USER>
<experiment1>
<colour>
<angle>
<experiment2>
<colour>
<angle>

etc.. hope this intuitive figure explains what my words don't :)
EDIT: the spaces are not shown...its now a bad figure.
Krodil
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Oct 31, 2011 7:36 pm

Re: Write a simple XML file in the SD card using XmlSerializ

Postby piciujupiter » Fri Nov 04, 2011 10:41 am

E/IOException( 359): exception in createNewFile() method
E/FileNotFoundException( 359): can't create FileOutputStream
E/Exception( 359): error occurred while creating xml file

why do I get this exceptions on this code?
piciujupiter
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 04, 2011 10:37 am

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests