• Android数据存储之XmlPull解析XML文件(读取部分)


    由于DOM解析xml文档需要将整个xml DOM树读入,当文件很大时,这种解析效率很低,而Android自带有一个事件触发型的xml解析器XmlPullParser,与SAX解析方式比较相似,区别在于XmlPullParser允许主动从解析器获取事件,满足条件后结束解析。

    首先写XmlPullUtil类(用途:操作解析器),成员变量与构造方法:

    public class XmlPullUtil {
        // 准备XmlUllParser需要的InputStream
        private InputStream inputStream = null;
        private XmlPullParserFactory xppFac = null;
        private XmlPullParser xpp = null;
        
        public XmlPullUtil(InputStream inputStream) {
            this.inputStream = inputStream;
        }

    其中的getAllPerson()方法实现解析文档,返回一个欲解析节点的对象列表;

    View Code
        public List<Person> getAllPerson() throws XmlPullParserException, IOException {
            // 准备好Person列表与Person对象引用(在xml文件中为Person节点)
            List<Person> persons = null;
            Person person = null;
            String elementName = null; // 节点名
            
                // 实例化XmlPullParserFactory与XmlPullParser
                xppFac = XmlPullParserFactory.newInstance();
                xpp = xppFac.newPullParser();
                xpp.setInput(inputStream, "UTF-8");
                
                /* 
                   XmlPullParser在解析过程中会返回一个表示状态的int类型,
                   由我们自己判断状态然后进行操作, 接着使用next方法移到
                   下一个位置继续判断
                */
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_DOCUMENT) {
                        // 文档开始处,实例化Person列表
                        persons = new ArrayList<Person>();
                    } else if (eventType == XmlPullParser.START_TAG) {
                        // 标签开始处,获得标签名
                        elementName = xpp.getName();
                        // 判断标签是不是Person,若是,实例化一个新Person
                        if ("person".equals(elementName)) {
                            person = new Person();
                        }
                    } else if (eventType == XmlPullParser.TEXT) {
                        if ("id".equals(elementName)) {
                            person.setId(xpp.getText());
                        } else if ("name".equals(elementName)) {
                            person.setName(xpp.getText());
                        }
                    } else if (eventType == XmlPullParser.END_TAG) {
                        elementName = xpp.getName();
                        // 判断结束标签是不是Person,若是, 将person加入列表
                        if ("person".equals(elementName)) {
                            persons.add(person);
                            person = null;
                        }
                    }
                    eventType = xpp.next();
                }
            return persons;
        }
        
    }

    然后在主Activity中就很简单了:

    View Code
    public class MainActivity extends Activity {
        
        private TextView idInfoText = null;
        private TextView nameInfoText = null;
        private Button readXmlBtn = null;
        
        private File file = null;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            idInfoText = (TextView) findViewById(R.id.idInfoText);
            nameInfoText = (TextView) findViewById(R.id.nameInfoText);
            readXmlBtn = (Button) findViewById(R.id.readXmlBtn);
            
            readXmlBtn.setOnClickListener(new ReadXmlBtn());
            
        }
        
        private class ReadXmlBtn implements OnClickListener {
    
            @Override
            public void onClick(View v) {
                // 首先调用检测储存环境的方法
                if(!MainActivity.this.CheckEnvironment()) {
                    return;
                } else {
                    try {
                        // 实例化XmlPullUtil类
                        XmlPullUtil xpu = new XmlPullUtil(new FileInputStream(file));
                        // 获取persons列表
                        List<Person> persons = xpu.getAllPerson();
                        // 显示结果
                        MainActivity.this.idInfoText.setText(persons.get(0).getId());
                        MainActivity.this.nameInfoText.setText(persons.get(0).getName());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (XmlPullParserException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    
                }
            }
            
        }
        
        /* 检测储存环境是否配置正常,并设置文件存放路径 */
        private boolean CheckEnvironment() {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "documents"
                        + File.separator + "shuai.xml");
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                return true;
            } else {
                return false;
            }
        }
        
    }
  • 相关阅读:
    select/poll/epoll 对比
    I/O Mutiplexing poll 和 epoll
    Socket 编程IO Multiplexing
    ubuntu12.04 lts 安装gcc 4.8
    time since epoch
    ceph-RGW Jewel版新概念
    支持向量机(svm)
    MachineLearning之Logistic回归
    ML之回归
    ML之监督学习算法之分类算法一 ——— 决策树算法
  • 原文地址:https://www.cnblogs.com/moka/p/3068731.html
Copyright © 2020-2023  润新知