XmlPullParser 解析XML

1.使用XmlPullParser解析xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

2.代码执行


            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser parser = factory.newPullParser();

            InputStream inputStream = getAssets().open("activity_main.xml");
            parser.setInput(inputStream, null);

            int type =0;
            //next() 游标指向下一个对象,同时获取类型
            while ((type = parser.next())!=XmlPullParser.END_DOCUMENT){


                switch (type){
                    case XmlPullParser.START_TAG:
                        Log.d(TAG, "START_TAG: ");
                        String name = parser.getName();
                        Log.d(TAG, "控件名称: "+name);
                        int nscount = parser.getNamespaceCount(1);
                        for (int i = 0; i < nscount; i++) {
                            //获取命名前缀 通过前缀获取值
                            String nspace = parser.getNamespacePrefix(i)+ "="+parser.getNamespace(parser.getNamespacePrefix(i));
                            Log.d(TAG, "命名空间: "+nspace);
                        }
                        //通过属性数量,获取每个属性
                        int count = parser.getAttributeCount();
                        for (int i = 0; i < count; i++) {
                            String s =parser.getAttributePrefix(i)+":"+ parser.getAttributeName(i)+"="+parser.getAttributeValue(i);
                            Log.d(TAG, "属性:"+s);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        Log.d(TAG, "END_TAG: ");
                        break;
                    case XmlPullParser.START_DOCUMENT:
                        Log.d(TAG, "START_DOCUMENT: ");
                        break;
                    case XmlPullParser.TEXT:
                        Log.d(TAG, "TEXT: ");
                        break;
                }
            }

3.结果

   16914-16914/? D/MainActivity: START_TAG: 
   16914-16914/? D/MainActivity: 控件名称: androidx.constraintlayout.widget.ConstraintLayout
   16914-16914/? D/MainActivity: 命名空间: android:http://schemas.android.com/apk/res/android
   16914-16914/? D/MainActivity: 命名空间: app:http://schemas.android.com/apk/res-auto
   16914-16914/? D/MainActivity: 命名空间: tools:http://schemas.android.com/tools
   16914-16914/? D/MainActivity: 属性:android:layout_width:match_parent
   16914-16914/? D/MainActivity: 属性:android:layout_height:match_parent
   16914-16914/? D/MainActivity: 属性:tools:context:.MainActivity
   16914-16914/? D/MainActivity: TEXT: 
   16914-16914/? D/MainActivity: START_TAG: 
   16914-16914/? D/MainActivity: 控件名称: TextView
   16914-16914/? D/MainActivity: 命名空间: android:http://schemas.android.com/apk/res/android
   16914-16914/? D/MainActivity: 命名空间: app:http://schemas.android.com/apk/res-auto
   16914-16914/? D/MainActivity: 命名空间: tools:http://schemas.android.com/tools
   16914-16914/? D/MainActivity: 属性:android:layout_width:wrap_content
   16914-16914/? D/MainActivity: 属性:android:layout_height:wrap_content
   16914-16914/? D/MainActivity: 属性:android:text:Hello World!
   16914-16914/? D/MainActivity: 属性:app:layout_constraintBottom_toBottomOf:parent
   16914-16914/? D/MainActivity: 属性:app:layout_constraintLeft_toLeftOf:parent
   16914-16914/? D/MainActivity: 属性:app:layout_constraintRight_toRightOf:parent
   16914-16914/? D/MainActivity: 属性:app:layout_constraintTop_toTopOf:parent
   16914-16914/? D/MainActivity: END_TAG: 
   16914-16914/? D/MainActivity: TEXT: 
   16914-16914/? D/MainActivity: END_TAG: 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值