XmlPullParser 不适用于 InputStream [英] XmlPullParser is not working with InputStream

查看:23
本文介绍了XmlPullParser 不适用于 InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 android 应用程序中使用 XmlPullParser 进行 xml 解析,但是当我将输入设置为 InputStream 时它不起作用,而我将输入设置为 Reader 它开始工作

I am using XmlPullParser for xml parsing in my android app but when I set input as InputStream it not works while I set input as Reader it starts working

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(obj,null);//obj is the object of InputStream
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
                 logger.println("eventType.."+eventType);
              if(eventType == XmlPullParser.START_DOCUMENT) {

                     // control goes here only

              } else if(eventType == XmlPullParser.START_TAG) {
                  //This block never executed
                  }

              } else if(eventType == XmlPullParser.END_TAG) {
                 //This block never executed
              } else if(eventType == XmlPullParser.TEXT) {

              }
              eventType = xpp.next();
             }

即使我将来自 InputStream 对象的数据存储在一个字符串中并将该字符串设置为输入,那么这段代码也可以正常工作.

Even if I store data from InputStream object in a string and set that String as input then this code also works fine.

xpp.setInput(new StringReader(str));//str contains the data from InputStream

推荐答案

同样的问题:直接传递 InputStream 在 Android 2.3.3 上可以正常工作,但在 4.1 上不行.您可以使用 xpp.setInput(new InputStreamReader(obj));

The same problem: passing InputStream directly works fine on Android 2.3.3 but doesn't work on 4.1. You can use xpp.setInput(new InputStreamReader(obj));

这篇关于XmlPullParser 不适用于 InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆