Saturday, 15 September 2012

dom - Failed to fetch xml data from res in Android 3.0 and above -



dom - Failed to fetch xml data from res in Android 3.0 and above -

similar issue

having few predefined xml kept under res>raw>first.xml fetch @ runtime , displying info below:

nodelist nodes = mainactivity.commonmethod.getdocumentfile(productactivity.this,_introwid).getelementsbytagname("string"); (int = 0; < nodes.getlength(); i++) { element e = (element)nodes.item(i); e.normalize(); _arrproductname.add( mainactivity.commonmethod.getvalue(e, "string")); }

method xml file(plist file) using document

public document getdocumentfile(context context, int rawid) { documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); builderfactory.setnamespaceaware(true); documentbuilder builder = null; seek { builder = builderfactory.newdocumentbuilder(); document = builder.parse(context.getresources().openrawresource( rawid)); document.getdocumentelement().normalize(); } grab (exception e1) { // todo auto-generated grab block e1.printstacktrace(); } homecoming document; }

getvalue method

public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); seek { stringwriter sw = new stringwriter(); transformer serializer = transformerfactory.newinstance().newtransformer(); serializer.transform(new domsource(n.item(0)), new streamresult(sw)); string result = sw.tostring(); system.out.println("result="+result); } grab (exception e) { e.printstacktrace(); } homecoming commonmethod.getelementvalue(n.item(0)); } public final static string getelementvalue(node elem) { node kid; if (elem != null) { if (elem.haschildnodes()) { (kid = elem.getfirstchild(); kid != null; kid = kid .getnextsibling()) { if (kid.getnodetype() == node.text_node) { homecoming kid.getnodevalue(); } } } } homecoming ""; }

test.xml

<?xml version="1.0" encoding="utf-8"?> <array> <!-- prdocuname --> <string>android ics 4.0™</string> <!-- prdocutdescription --> <string>mobile</string> <!-- prdocuimage --> <string>mobile.png</string> <!-- prdocuaddress --> <string>url</string> <!-- conversion --> <integer>400</integer> <!-- thicknessnames --> <string>skim</string> <!-- thicknessvalues --> <string>1</string> <!-- longdescription --> <string>android market leader in terms of total number of device sold , leader in terms of total number of application available in market.</string> </array>

above entire code work below 4.0 not above 4.0, getelementsbytagname` homecoming null result above 4.0.

result in below 4.0

<?xml version="1.0" encoding="utf-8"?><string>android ics 4.0™</string>

result in above 4.0

<?xml version="1.0" encoding="utf-8"?>

string tag missing while testing in above 4.0,

finally achieved, made changes within getvaluefunction;

change code:

public string getvalue(element item, string str) { string strresponse=""; node kid; if(item!=null) { if(item.haschildnodes()) { for(kid=item.getfirstchild(); kid!=null; kid =kid.getnextsibling()) { if (kid.getnodetype() == node.text_node) { strresponse =kid.getnodevalue(); homecoming strresponse; } } }else { nodelist n = item.getelementsbytagname(str); n = item.getchildnodes(); if(((node) n.item(0))!=null) { if(((node) n.item(0)).getnodevalue() !=null) { strresponse =((node) n.item(0)).getnodevalue(); homecoming strresponse; }else { strresponse =""; } } } } homecoming strresponse; }

its works perfectly!

android dom xml-parsing

No comments:

Post a Comment