ICS中的XML解析器错误?

时间:2012-08-31 05:12:09

标签: android xml-parsing android-4.0-ice-cream-sandwich

我正在尝试使用 XMLPullFeedParser 解析xml,除 ICS 外所有设备都正常工作。

我这样的xml格式,

<?xml version="1.0" encoding="utf-8"?>
   <Result>
       <Tag>
         <Name>
           Bangalore
         </Name>
       </Tag>
       <Tag>
         <Name>
           Chennai
         </Name>
       </Tag>
    </Result>

Logcat错误,

08-31 10:36:31.988: ERROR/Error Mian PullFeedParser(12056): Invalid stream or encoding: java.io.IOException: Attempted read on closed stream. (position:START_DOCUMENT null@1:1) caused by: java.io.IOException: Attempted read on closed stream.
08-31 10:36:31.988: ERROR/Error Mian PullFeedParser(12056): org.xmlpull.v1.XmlPullParserException: Invalid stream or encoding: java.io.IOException: Attempted read on closed stream. (position:START_DOCUMENT null@1:1) caused by: java.io.IOException: Attempted read on closed stream.
08-31 10:36:31.988: ERROR/Error Mian PullFeedParser(12056):     at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1730)

我的解析器:

公共列表解析器(InputStream responseStream){

    // TODO Auto-generated method stub
    List<Message> messages = null;
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(responseStream, null);
        int eventType = parser.getEventType();
        Message currentMessage = null;
        boolean done = false;
        while (eventType != XmlPullParser.END_DOCUMENT && !done){
            String name = null;
            switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                messages = new ArrayList<Message>();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();

                if (name.equalsIgnoreCase("Name")){
                    currentMessage = new Message();
                    currentMessage.setName(parser.nextText());
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("Name")&& currentMessage != null){
                    messages.add(currentMessage);
                }
                else if(name.equalsIgnoreCase("Result")){
                    done = true;
                }
                break;
            }
            eventType = parser.next();
        }
    } catch (Exception e) {
        Log.e("Error Mian PullFeedParser", e.getMessage(), e);
    }
    return messages;
}

让我知道解析这种XML格式。

感谢。

0 个答案:

没有答案