简体   繁体   中英

XmlPullParserException during parsing XML with XMLPullParser

Need your help to resolve this : Trying to parse this 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 a 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 to 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 is the name attribute of configuration tag. But getting an exception -

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}configuration (position:START_DOCUMENT null@1:1 in java.io.InputStreamReader@42323800) 

Please help me where I am going wrong.

Make these two changes in your code:

  1. Make xml valid. The segment tags are not ended. They should be: <segment order="1" name="left upper anterior"/>

  2. Remove the line parser.require(XmlPullParser.START_TAG, null, "configuration");

Everything else looks fine. I was able to run your code as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM