android create and read xml from sdcard -
i use 2 button, 1st button create xml file in sdcard
, 2nd button read xml.
code :
string xml_root; listview lv_emp; static final string key_name = "name"; static final string key_age = "age"; list<hashmap<string, string>> emphashmap; list<classemp> emplist = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_empacc); xml_root = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\n" + "<employees>" + "\n" + "\t" + "<emp>" + "\n" + "\t" + "\t" + "<name>" + "jame" + "</name>" + "\n" + "\t" + "\t" + "<age>" + "17" + "</age>" + "\n" + "\t" + "</emp>" + "\n" + "\t" + "<emp>" + "\n" + "\t" + "\t" + "<name>" + "natalie" + "</name>" + "\n" + "\t" + "\t" + "<age>" + "20" + "</age>" + "\n" + "\t" + "</emp>" + "\n" + "</employees>"; button bt_create2 = (button)findviewbyid(r.id.bt_createfilet2); bt_create2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { file newfolder = new file(environment.getexternalstoragedirectory(), "testfolder"); if (!newfolder.exists()) { newfolder.mkdir(); } file file = new file(newfolder, "test" + ".xml"); if (!file.exists()) { file.createnewfile(); fileoutputstream fout = new fileoutputstream(file); outputstreamwriter myoutwriter =new outputstreamwriter(fout); myoutwriter.append(xml_root); myoutwriter.close(); fout.close(); } } catch (exception e) { system.out.println("e: " + e); } } }); button bt_read2 = (button)findviewbyid(r.id.bt_readfilet2); bt_read2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { lv_emp = (listview)findviewbyid(r.id.lv_emp); file file = new file(environment.getexternalstoragedirectory() + "testfolder/test.xml"); inputstream = new fileinputstream(file.getpath()); xmlpullparseremp parser_emp = new xmlpullparseremp(); emplist = parser_emp.parse(is); binderdataemp bd_emp = new binderdataemp(empacc.this, emphashmap); lv_emp.setadapter(bd_emp); } catch (exception e){ e.printstacktrace(); } } }); }
with code, create test.xml in testfolder on sdcard.
can't read it.
how fix ?
you missing /(slash) during reading file
use , retry:
file file = new file(environment.getexternalstoragedirectory() + "/testfolder/test.xml");
Comments
Post a Comment