How to create and write to xml file in android
I'am beginner to android I have an emergency contact screen in that I have two fields like (email,phone number) I want to save these things in xml
instead of sqlite
. I used following code for saving but I unable to create xml file in internal memory,it's giving exception please see the code below.
also i used this code shown in code-2 below using that i unable to read values and how can i see the file is created or what please help me.
Advance thanks,
TextView txtemailid=(TextView)findViewById(R.id.EmailId);
TextView txtphoneno=(TextView)findViewById(R.id.phoneNo);
File newxmlfile = new File("/data/com.itwine/emergency.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 encod开发者_运维知识库ing 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");
*//**serializer.startTag(null, "Child1");
serializer.endTag(null, "Child1");
serializer.startTag(null, "Child2");
serializer.attribute(null, "attribute", "value");
serializer.endTag(null, "Child2");*//*
serializer.startTag(null, "EmailId");
serializer.text(txtemailid.getText().toString());
serializer.endTag(null,"EmailId");
serializer.startTag(null, "PhoneNo");
serializer.text(txtphoneno.getText().toString());
serializer.endTag(null,"PhoneNo");
serializer.endTag(null,"root");
serializer.endDocument();
//write xml data into the FileOutputStream
serializer.flush();
//finally we close the file stream
fileos.close();
Toast.makeText(getApplication(), "xml created",Toast.LENGTH_LONG);
} catch (Exception e) {
Log.e("Exception","error occurred while creating xml file");
}
code-2
String FILENAME = "hello_file"; String string = "hello world!";
FileOutputStream fos=null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.write(string.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Don't use that storage location.
Look at Android and data storage space?
I would suggest you have a look on SharedPreferences (the first link on the given page).
If I'm not completely mistaking you can choose where to store your data. Also notice the SharedPreferences.Editor for the actual writing (and especially the apply()
function instead of commit()
since the prior handles the actual file access in a separate thread for you, which makes it safe to call from the main thread).
精彩评论