2
回答
android使用XmlPullParser解析xml 一直无法执行START_TAG中的内容
private String xml2Bean(InputStream ins) {


        try {
            // 实例化XmlPullParserFactory
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            // 实例化XmlPullParser
            XmlPullParser parser = factory.newPullParser();

            // Sets the input stream the parser is going to process.
            // This call resets the parser state
            // and sets the event type  to the initial value START_DOCUMENT.
            parser.setInput(ins ,"UTF-8");

            int eventType = parser.getEventType();

            while(eventType != XmlPullParser.END_DOCUMENT){
                switch (eventType){
                    case XmlPullParser.START_DOCUMENT :
                        LogUtils.d("START_DOCUMENT");
                        break ;
                    case XmlPullParser.START_TAG:
                        LogUtils.d("START_TAG");
                        break ;
                    case XmlPullParser.END_TAG:
                        LogUtils.d("END_TAG");
                        break ;
                    default:
                        LogUtils.d("default");
                        break;
                }
                // Get next parsing event
                eventType = parser.next();
                LogUtils.d("eventType:" + eventType);
            }
            return "1111" ;
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }



我查看了下
XmlPullParser的源码,其中
int START_DOCUMENT = 0 ;
int END_DOCUMENT = 1;
int START_TAG = 2;
int END_TAG = 3;
int TEXT = 4;
。。。。。 通过
setInput()方法初始化将eventType初始化为 START_DOCUMENT = 0 ,然后在while循环里面调用 parser.next() ,这时eventType不就变为2了吗? 那不就是END_DOCUMENT ,直接退出循环了...求赐教
<无标签>
举报
小工匠go
发帖于3年前 2回/706阅
顶部