2 netboynew netboynew 于 2016.12.31 21:19 提问

android pull 解析XML ,出错求解!

package com.example.administrator.xml;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button_pullXml).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          ArrayList<Person> list =  pullXml();
            for (int i=0;i<list.size();i++){
                System.out.println(list.get(i));
            }

        }
        private ArrayList<Person>  pullXml() {
            ArrayList<Person> list = new ArrayList<Person>();
            Person person = null;
          //  创建XML解析器
            XmlPullParser pullParser = Xml.newPullParser();
            //创建解析器输入流
            InputStream in = getResources().openRawResource(R.raw.person);
            try {
                pullParser.setInput(in,"UTF-8");
                int eventType = pullParser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT){
                    switch (eventType){
                        case XmlPullParser.START_TAG:
                            String tag = pullParser.getName();
                            if("person".equals(tag)){
                                person = new Person();
                                person.setId(Integer.parseInt((pullParser.getAttributeValue(null,"id"))));
                            }else  if("name".equals(tag)){
                                person.setName(pullParser.nextText());
                            }else  if("age".equals(tag)){
                                person.setAge(Integer.parseInt(pullParser.nextText()));
                            }else if("sex".equals(tag)){
                                person.setSex(pullParser.nextText());
                            }else if("tel".equals(tag)){
                                person.setTel(pullParser.nextText());
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            list.add(person);
                            break;
                    }
                    pullParser.next();
                }
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return list;
        }
    });
}

}

运行后,点击解析BUTTON,卡顿很久提示Xml文件无响应,请教是哪里出错了。

2个回答

sinat_37181010
sinat_37181010   2016.12.31 21:40

你可以关注下 ITIL先锋这个微信公众号 上面每天会更新IT行业最前沿资讯和各种类别的IT资料,感觉工作学习都比较有用

oyljerry
oyljerry   Ds   Rxr 2016.12.31 23:00

先看看你xml解析到哪一步了?是不是死循环了,或者XML太大解析很消耗性能

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!