How to create and save data to XML file in android?

Hawke001

New Member
I have the following code which suppose to create an XML file and save data on Android The code is:\[code\]try { File newxmlfile= new File(Environment.getExternalStorageDirectory()+"sos.xml"); newxmlfile.getParentFile().mkdirs(); FileOutputStream fileos = new FileOutputStream(newxmlfile); XmlSerializer xmlSerializer = Xml.newSerializer(); //StringWriter writer = new StringWriter(); xmlSerializer.setOutput(fileos,"UTF-8"); xmlSerializer.startDocument(null,true); xmlSerializer.startTag(null, "Contact"); xmlSerializer.startTag(null, "FirstName"); xmlSerializer.text(first.getText().toString()); xmlSerializer.endTag(null,"FirstName"); xmlSerializer.startTag(null,"LastName"); xmlSerializer.text(last.getText().toString()); xmlSerializer.endTag(null, "LastName"); xmlSerializer.startTag(null, "Phone"); xmlSerializer.text(phone.getText().toString()); xmlSerializer.endTag(null, "Phone"); xmlSerializer.startTag(null, "Email"); xmlSerializer.text(email.getText().toString()); xmlSerializer.endTag(null, "Email"); xmlSerializer.endTag(null, "Contact"); xmlSerializer.endDocument(); xmlSerializer.flush(); fileos.close();} catch (Exception e) { Log.e("Exception","error occurred while creating xml file");} \[/code\]and I specify the user-permission in the manifest file as follow:\[code\]<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />\[/code\]When I test the code above in real device it throw the exception that I already declare in try catch clause, which is\[quote\] E/Exception(9677): error occurred while creating xml file\[/quote\]Can some one provide me with clue about what I am doing wrong here?Any help or redirection will be greatly appreciated.
 
Back
Top