java - XmlPullParserException during parsing XML with XMLPullParser -
need resolve : trying parse xml -
<lungprotocol><configuration name="flus sitting"> <segment order="1" name="left upper anterior"> <segment order="2" name="left lower anterior"> </configuration> <configuration name="flus supine"> <segment order="1" name="left upper anterior"> <segment order="2" name="left lower anterior"> </configuration></lungprotocol>
with following function -
public list<lungsegment> parse(inputstream is, string configuration) { try { xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser parser = factory.newpullparser(); segment = new lungsegment(); parser.setinput(is, null); parser.require(xmlpullparser.start_tag, null, "configuration"); int eventtype = parser.geteventtype(); while (eventtype != xmlpullparser.end_document) { string tagname = parser.getname(); switch (eventtype) { case xmlpullparser.start_tag: if(("configuration").equalsignorecase(tagname) && parser.getattributevalue(null, "name").equalsignorecase(configuration)){ eventtype = parser.next(); //eventtype = parser.next(); tagname = parser.getname(); log.v("xmltag", "configuration = "+configuration); if (("segment").equalsignorecase(tagname)) { // create new instance of segment segment = new lungsegment(); segment.setsegmentname(parser.getattributevalue(null, "name")); segment.setsegmentorder(integer.parseint(parser.getattributevalue(null, "order"))); } } break; case xmlpullparser.end_tag: if (tagname.equalsignorecase("segment")) { // add segment object list segments.add(segment); } else if (("configuration").equalsignorecase(tagname) && parser.getattributevalue(null, "name").equalsignorecase(configuration)) { return segments; } break; } eventtype = parser.next(); } } catch (xmlpullparserexception e) {e.printstacktrace();} catch (ioexception e) {e.printstacktrace();} return segments; }
where configuration name attribute of configuration tag. getting exception -
org.xmlpull.v1.xmlpullparserexception: expected: start_tag {null}configuration (position:start_document null@1:1 in java.io.inputstreamreader@42323800)
please me going wrong.
make these 2 changes in code:
make xml valid. segment tags not ended. should be:
<segment order="1" name="left upper anterior"/>
remove line
parser.require(xmlpullparser.start_tag, null, "configuration");
everything else looks fine. able run code well.
Comments
Post a Comment