xml - Android : Parse using xmlpullparser -
i have xml file in location res/xml/data.xml
need parse xml file
xmlresourceparser xrp=context.getresources().getxml(r.xml.data);
i used code file. returns xmlresourceparser
tried xmlpullparser
xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser();
i not getting clear idea between these 2 parser. question how parse xml file in resource folder using xmlpullparser
?
xmlresourceparser
interface
extends xmlpullparser
.
getxml
wil return xmlresourceparser
object. can read parser text similar how parse input stream or string using xmlpullparser
here sample code parse resource xml
try { xmlresourceparser xmlresourceparser = getresources().getxml(r.xml.data); int eventtype = xmlresourceparser.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 " + xmlresourceparser.getname()); } else if (eventtype == xmlpullparser.end_tag) { system.out.println("end tag " + xmlresourceparser.getname()); } else if (eventtype == xmlpullparser.text) { system.out.println("text " + xmlresourceparser.gettext()); } eventtype = xmlresourceparser.next(); } system.out.println("end document"); } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment