Getting "null" in place of textvalue After parsing XML using PullParser in Android -
i had @ this. please dont redirect me site.
i had used dom parser , here problem that.
i having same xml :
<myresource> <item>first</item> <item>second</item> </myresource>
and have no way parse xml via pullparser. methods taken this:
private list readfeed(xmlpullparser parser) throws xmlpullparserexception, ioexception { // todo auto-generated method stub list entries = new arraylist(); parser.require(xmlpullparser.start_tag, ns, "myresource"); while (parser.next() != xmlpullparser.end_tag) { if (parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); // starts looking entry tag if (name.equals("myresource")) { entries.add(readentry(parser)); } else { skip(parser); } } return entries; } private void skip(xmlpullparser parser) throws xmlpullparserexception, ioexception { // todo auto-generated method stub if (parser.geteventtype() != xmlpullparser.start_tag) { throw new illegalstateexception(); } int depth = 1; while (depth != 0) { switch (parser.next()) { case xmlpullparser.end_tag: depth--; break; case xmlpullparser.start_tag: depth++; break; } } } private item readentry(xmlpullparser parser) throws xmlpullparserexception, ioexception { // todo auto-generated method stub parser.require(xmlpullparser.start_tag, ns, "item"); string text = null; while (parser.next() != xmlpullparser.end_tag) { if (parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); if (name.equals("item")) { text = readmytext(parser); } else { skip(parser); } } return new item(text); } private string readmytext(xmlpullparser parser) throws xmlpullparserexception, ioexception { // todo auto-generated method stub parser.require(xmlpullparser.start_tag, ns, "item"); string text = readtext(parser); parser.require(xmlpullparser.end_tag, ns, "item"); return text; } private string readtext(xmlpullparser parser) throws xmlpullparserexception, ioexception { string result = ""; if (parser.next() == xmlpullparser.text) { result = parser.gettext(); parser.nexttag(); } return result; }
i getting parsed text :
null null
can me out of ??
refer this link having example
xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); xpp.setinput(new stringreader ("<foo>hello world!</foo>")); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { if(eventtype == xmlpullparser.start_document) { system.out.println("start document"); } else if(eventtype == xmlpullparser.end_document) { system.out.println("end document"); } else if(eventtype == xmlpullparser.start_tag) { system.out.println("start tag "+xpp.getname()); } 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(); }
Comments
Post a Comment