Thursday, January 14, 2010

Read, Write and Parse XML file in Android

1. Read and Parse Xml file
The sample XML file looks like the following:
<packingList>
<category name="test">
</category>
<packingList>


The following code shows you how to read and parse the file
ArrayList<Category> categories;
XmlPullParser parser = Xml.newPullParser();
try {
FileInputStream fIn = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fIn);

// auto-detect the encoding from the stream
parser.setInput(isr);
int eventType = parser.getEventType();
Category currentCategory = null;
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("packingList")){
categories = new ArrayList<Category>();
} else if (categories != null){
if (name.equalsIgnoreCase("category"))
currentCategory = new Category(parser.getAttributeValue(0));
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("category") &&
currentCategory != null){
categories.add(0, currentCategory);
} else if (name.equalsIgnoreCase("packingList")){
done = true;
}
break;
}
eventType = parser.next();
}
} catch (FileNotFoundException e) {
// TODO
} catch (IOException e) {
// TODO
} catch (Exception e){
// TODO
}


2. Write to file
 
try {
FileOutputStream fOut = openFileOutput(FILE_NAME, MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
String fileContent = …; //build file content
osw.write(fileContent );
osw.flush();
osw.close();

} catch (FileNotFoundException e) {

}


 

12 comments:

  1. Will you please tell me where u put your XML file in the application directory?

    ReplyDelete
  2. you can added in Ressources/xml folder and you can call it getString(R.xml.XMLfile)

    ReplyDelete
  3. ArrayList <<< You need a Class for this right?

    ReplyDelete
  4. The Category.java looks like this:
    public class Category{
    private String name;
    public getName(){
    return name;
    }
    public setName(String name){
    this.name = name;
    }
    public Category(String name){
    setName(name);
    }
    }

    ArrayList is a Java class. You need to import java.util.ArrayList;

    ReplyDelete
  5. how to create a programs; create a text view in main.xml, reading and writing in local xml file with automatically updating local xml file on android

    ReplyDelete
  6. this code is giving error of initializing categories.so plz tell me how to remove it??

    ReplyDelete
  7. You will need to have Category.java added to your project. See my answer above for what the Category.java looks like.

    ReplyDelete
  8. I,m Beginner for learning android ,so i used tihs code and retrieved Arraylist from the method ..now i want to use the retrieved list.. for example , retrieve value from it in EditText ..Thanks in Advance

    ReplyDelete
  9. I,m Beginner for learning android ,so i used tihs code and retrieved Arraylist from the method ..now i want to use the retrieved list.. for example , retrieve value from it in EditText ..Thanks in Advance

    ReplyDelete
    Replies
    1. You may check this example: http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html

      Delete