xml string parse in android -
i new xml parsing in android. below xml string have. getting xml string data onactivityresult method.so below data saved in 1 string variable resulttext; want read each attribute values below string.
<?xml version="1.0" encoding="utf-8"?> <printletterbarcodedata uid="521007171049" name="bandigari katamaraju" gender="m" yob="1991" co="s/o: bandigari yadagiri" house="4-141/1" loc="edullagudem" vtc="edullagudam" dist="nalgonda" subdist="for new vtc" state="andhra pradesh" pc="508112"/>
i want read each attribute of single node. in advance.
use xmlparser
(check this page), 1 works nicely xmlpullparser.
you can initialize parser with:
xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); xpp.setinput( new stringreader ( "<foo>hello world!</foo>" ) );
than can iterate on complete xml
object using
int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { /*your code here*/ eventtype = xpp.next(); }
where can check eventtypes
: start_document
, start_tag
, end_tag
, , text
.
once in either start or end tag can name of tag using getname()
, @ text
event type can use gettext()
, can use, @ begin_tag
, functions getattributecount()
, getattributename(index)
, getattributevalue()
can attributes belonging each tag.
in specific case
you can use this:
string xmlstring = yourstring_here; xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); xpp.setinput( new stringreader ( xmlstring ) ); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { if(eventtype == xmlpullparser.start_document) { system.out.println("start document"); } else if(eventtype == xmlpullparser.start_tag) { system.out.println("start tag "+xpp.getname()); if (xpp.getname().equals("printletterbarcodedata")){ (int i=0; i<xpp.getattributecount(); i++){ system.out.println("attribute:"+xpp.getattributename(i)+" value: "+xpp.getattributevalue(i)) //store here values in variables of choice. } } } else if(eventtype == xmlpullparser.end_tag) { system.out.println("end tag "+xpp.getname()); } else if(eventtype == xmlpullparser.text) { system.out.println("text "+xpp.gettext()); } eventtype = xpp.next(); } system.out.println("end document");
Comments
Post a Comment