Open XML file from res/xml in Android -
i created java application opens xml file looks this:
<animaltree> <animal> <mammal>canine</mammal> <color>blue</color> </animal> <!-- ... --> </animaltree>
and can open using:
file fxmlfile = getresources.getxml("res/xml/data.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse(fxmlfile); doc.getdocumentelement().normalize(); nodelist animalnodes = doc.getelementsbytagname("animal");
then can create node, push object listarray, want objects loop through listarray.
for (int temp = 0; temp < animalnodes.getlength(); temp++) { node nnode = animalnodes.item(temp); if (nnode.getnodetype() == node.element_node) { element eelement = (element) nnode; question thisanimal = new animal(); thisanimal.mammal = gettagvalue("mammal",eelement); // ...
plain , simple! only, in android cannot read file "res/xml/data.xml
" because "file();
" requires string
not integer
(id). lost. there way can make "file();
" open file, or impossible without using saxparser
or xpp
? (both of cannot understand, no matter how hard try.)
if forced use methods, can show me simple code analogous example?
if it's in resource tree, it'll id assigned it, can open stream openrawresource function:
inputstream = context.getresources().openrawresource(r.xml.data);
as working xml in android, this link on ibm.com incredibly thorough.
see listing 9. dom-based implementation of feed parser
in link.
once have input stream (above) can pass instance of documentbuilder:
documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); document dom = builder.parse(this.getinputstream()); element root = dom.getdocumentelement(); nodelist items = root.getelementsbytagname("thetagyouwant");
keep in mind, haven't done -- i'm assuming code provided ibm works.
Comments
Post a Comment