安卓生成XML以及PULL解析XML詳解


第一步,先建一个布局,很简单,注意下,我布局里使用的Edittext不是原生的,是使用的一个第三方包,感兴趣的可以去我的上一篇文章里看一下,代码如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main5Activity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#000"
        android:text="账号注册"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>



    <scut.carson_ho.diy_view.SuperEditText
        android:id="@+id/ed_names"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="15dp"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:layout_marginRight="15dp"
        android:hint="请输入账号:"/>

    <scut.carson_ho.diy_view.SuperEditText
        android:id="@+id/ed_passs"
        android:inputType="numberPassword"
        android:paddingLeft="5dp"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:hint="请输入密码:"/>



    <Spinner
        android:id="@+id/sp_sexs"
        android:inputType="numberPassword"
        android:paddingLeft="5dp"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:entries="@array/item"
        android:layout_marginRight="15dp"
        android:hint="请输入密码:"/>



    <scut.carson_ho.diy_view.SuperEditText
        android:id="@+id/ed_phones"
        android:inputType="numberPassword"
        android:paddingLeft="5dp"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:hint="请输入手机号:"/>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">


        <Button
            android:id="@+id/logins"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"/>


        <Button
            android:id="@+id/chakan"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="15dp"
            android:layout_height="wrap_content"
            android:text="查看"/>

    </LinearLayout>


</LinearLayout>



第二步,找ID那些就不说了,解析也懒得写了,代码里都写了注释,直接上存储XML代码:


                // 得到xml序列化器对象
                XmlSerializer serializer = Xml.newSerializer();

                //给序列化器设置输出流
                File file = new File(getFilesDir().getAbsolutePath() + "my.xml");


                try {

                    //数据初始化
                    name = ed_name.getText().toString().trim();
                    pass = ed_pass.getText().toString().trim();
                    phone = ed_phone.getText().toString().trim();
                    sex = sp_sex.getSelectedItem().toString().trim();

                    //给序列化器指定好输出流
                    FileOutputStream fos = new FileOutputStream(file);

                    //这个utf-8的作用,是指定xml文件用什么编码生成
                    serializer.setOutput(fos, "UTF-8");

                    //这个utf-8的作用,指定的是头结点中encoding这个属性的值
                    serializer.startDocument("UTF-8", true);
                    serializer.startTag(null, "User");

                    //根节点的开始标签
                    serializer.startTag(null, "name");
                    serializer.text(name);
                    //根节点的结束标签
                    serializer.endTag(null, "name");


                    serializer.startTag(null, "pass");
                    serializer.text(pass);
                    serializer.endTag(null, "pass");


                    serializer.startTag(null, "phone");
                    serializer.text(phone);
                    serializer.endTag(null, "phone");


                    serializer.startTag(null, "sex");
                    serializer.text(sex);
                    serializer.endTag(null, "sex");

                    serializer.endTag(null, "User");
                    serializer.endDocument();

                    serializer.flush();
                    fos.close();

                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                }


第三步,存完了当然就该取了,取的时候我拿的PULL解析的,至于为什么拿pull解析自己去百度。直接上代码,代码中写了注释:

 File files = new File(getFilesDir().getAbsolutePath() + "my.xml");
                try {
                    FileInputStream fis = new FileInputStream(files);
                    //获取Xml的pull解析器
                    XmlPullParser xp = Xml.newPullParser();

                    //指定使用什么编码去解析xml文件
                    xp.setInput(fis,"UTF-8");

                    //开始解析
                    //需要判断当前解析到什么标签,因为解析到不同的标签需要做不同的操作
                    //通过获取当前标签的事件类型
                    int type = xp.getEventType();

                    while (type!= XmlPullParser.END_DOCUMENT){
                        switch (type){
                            case  XmlPullParser.START_TAG:
                                if("User".equals(xp.getName())){//获取当前标签的名字
                                    mList = new ArrayList<Bean>();
                                    bean =new Bean();
                                }else if("name".equals(xp.getName())){
                                    String name = xp.nextText();//获取当前节点的下一个节点的文本,并把指针移动至当前节点的结束节点
                                    bean.setName(name);
                                }else if("pass".equals(xp.getName())){
                                    String pass = xp.nextText();
                                    bean.setPass(pass);
                                }else if("sex".equals(xp.getName())){
                                    String sex = xp.nextText();
                                    bean.setSex(sex);
                                }else if("phone".equals(xp.getName())){
                                    String phone = xp.nextText();
                                    bean.setSex(phone);
                                }
                                break;
                            case XmlPullParser.END_TAG:
                                if("User".equals(xp.getName())){
                                    mList.add(bean);
                                }
                                break;
                        }
                        //指针移动至下一个节点,并且获取它的事件类型
                        type = xp.next();
                    }
                    //解析结果

                    for (int i = 0; i < mList.size(); i++) {
                        Log.e("mlddddddddist",mList.get(i).getName()+mList.get(i).getPass()+mList.get(i).getSex());
                        Log.e("TAGcvvvvvvvvvv", "onClick: "+mList.size() );
                    }

                } catch (Exception e) {


                }
OK,就这么简单,希望能对需要的人提供一点帮助。

猜你喜欢

转载自blog.csdn.net/haojiagou/article/details/80524507
今日推荐