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) {
…
}
Will you please tell me where u put your XML file in the application directory?
ReplyDeleteyou can added in Ressources/xml folder and you can call it getString(R.xml.XMLfile)
ReplyDeletewhat's in your Category.java?
ReplyDeleteArrayList <<< You need a Class for this right?
ReplyDeleteThe Category.java looks like this:
ReplyDeletepublic 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;
Thanks, helped alot :D
ReplyDeletehow 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
ReplyDeletethis code is giving error of initializing categories.so plz tell me how to remove it??
ReplyDeleteYou will need to have Category.java added to your project. See my answer above for what the Category.java looks like.
ReplyDeleteI,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
ReplyDeleteI,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
ReplyDeleteYou may check this example: http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html
Delete