i'm new @ android , trying local xml file , data , display .
here's code:
public string getxmldata() { documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); documentbuilder builder = null; try { builder = builderfactory.newdocumentbuilder(); inputstream = getassets().open("words.xml"); document document = builder.parse(new fileinputstream("c:\\users\\ocean\\androidstudioprojects\\deneme1project\\deneme1\\build\\res\\assets\\words.xml")); element rootelement = document.getdocumentelement(); nodelist nodes = rootelement.getchildnodes(); for(int i=0; i<nodes.getlength(); i++){ node node = nodes.item(i); if(node instanceof element) { //a child element process element child = (element) node; title = child.getattribute("title"); string author= child.getattribute("author"); string year= child.getattribute("year"); } } return title; } catch (exception e) { e.printstacktrace(); return e.tostring(); } }
my first question should put xml file? inside res created folder , named assets put inside have rs , r folders :s file not found exception (file there :p)
this points local computer, when deploy apk can't reference.
document document = builder.parse(new fileinputstream("c:\\users\\ocean\\androidstudioprojects\\deneme1project\\deneme1\\build\\res\\assets\\words.xml"));
try instead:
getassets().open("words.xml");
where words.xml in assets directory. work activity
, or if have context
can call getassets() on context, i.e. context.getassets()
.
Comments
Post a Comment