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: