2

我正在尝试使用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)

我的解析器:

公共列表解析器(输入流响应流){

    // 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 格式的想法。

谢谢。

4

0 回答 0