XmlPullParser 是 Android 平台上用于解析 XML 数据的一个接口,它提供了更高效的方式来读取 XML 文档
XmlPullParser 不会一次性将整个 XML 文档加载到内存中,而是采用流式读取的方式,即边读取边解析,这样可以大大减少对内存的使用,特别适合处理大型 XML 文件
XmlPullParser xmlPullParser = Xml.newPullParser();
InputStream inputStream = getResources().openRawResource(R.raw.users);
xmlPullParser.setInput(inputStream, null);
int eventType = xmlPullParser.getEventType();
int START_DOCUMENT = 0;
int END_DOCUMENT = 1;
int START_TAG = 2;
int END_TAG = 3;
int TEXT = 4;
事件 | 说明 |
---|---|
START_DOCUMENT | 表示文档开始 |
END_DOCUMENT | 表示文档结束 |
START_TAG | 表示开始标签 |
END_TAG | 表示结束标签 |
TEXT | 表示标签之间的文本内容 |
int next = xmlPullParser.next();
START_TAG
或 END_TAG
事件上调用时,才会返回标签的名称,否则返回 nullString name = xmlPullParser.getName();
String text = xmlPullParser.getText();
res/raw
目录下,这样,在 Activity 中可通过 getResources().openRawResource()
获取到该目录下的资源<users>
<user>
<name>jackname>
<age>21age>
user>
<user>
<name>tomname>
<age>22age>
user>
users>
InputStream inputStream = getResources().openRawResource(R.raw.users);
XmlPullParser xmlPullParser = Xml.newPullParser();
try {
xmlPullParser.setInput(inputStream, null);
Log.i(TAG, "------------------------------ 文档开始");
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 users 开始标签");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 users 标签之间的文本内容");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 user 开始标签");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 user 标签之间的文本内容");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 name 开始标签");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 name 标签之间的文本内容");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
Log.i(TAG, "------------------------------ 到 name 结束标签");
xmlPullParser.next();
Log.i(TAG, "--------------- " + xmlPullParser.getEventType());
Log.i(TAG, "--------------- " + xmlPullParser.getName());
Log.i(TAG, "--------------- " + xmlPullParser.getText());
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
I/GetXmlActivity: ------------------------------ 文档开始
I/GetXmlActivity: --------------- 0
I/GetXmlActivity: --------------- null
I/GetXmlActivity: --------------- null
I/GetXmlActivity: ------------------------------ 到 users 开始标签
I/GetXmlActivity: --------------- 2
I/GetXmlActivity: --------------- users
I/GetXmlActivity: --------------- null
I/GetXmlActivity: ------------------------------ 到 users 标签之间的文本内容
I/GetXmlActivity: --------------- 4
I/GetXmlActivity: --------------- null
I/GetXmlActivity: ---------------
I/GetXmlActivity: ------------------------------ 到 user 开始标签
I/GetXmlActivity: --------------- 2
I/GetXmlActivity: --------------- user
I/GetXmlActivity: --------------- null
I/GetXmlActivity: ------------------------------ 到 user 标签之间的文本内容
I/GetXmlActivity: --------------- 4
I/GetXmlActivity: --------------- null
I/GetXmlActivity: ---------------
I/GetXmlActivity: ------------------------------ 到 name 开始标签
I/GetXmlActivity: --------------- 2
I/GetXmlActivity: --------------- name
I/GetXmlActivity: --------------- null
I/GetXmlActivity: ------------------------------ 到 name 标签之间的文本内容
I/GetXmlActivity: --------------- 4
I/GetXmlActivity: --------------- null
I/GetXmlActivity: --------------- jack
I/GetXmlActivity: ------------------------------ 到 name 结束标签
I/GetXmlActivity: --------------- 3
I/GetXmlActivity: --------------- name
I/GetXmlActivity: --------------- null
对于
,我们只关注 name 标签之间的文本内容
即当 xmlPullParser.getEventType() == XmlPullParser.START_TAG
以及 xmlPullParser.getName().equals("name")
时,我们已经到 name 开始标签
这个时候再走一步,即可获取到 name 标签之间的文本内容,即 xmlPullParser.next()
之后 xmlPullParser.getText()
InputStream inputStream = getResources().openRawResource(R.raw.users);
XmlPullParser xmlPullParser = Xml.newPullParser();
try {
xmlPullParser.setInput(inputStream, null);
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = xmlPullParser.getName();
if (name.equals("name")) {
xmlPullParser.next();
String text = xmlPullParser.getText();
Log.i(TAG, "--------------- name: " + text);
} else if (name.equals("age")) {
xmlPullParser.next();
String text = xmlPullParser.getText();
Log.i(TAG, "--------------- age: " + text);
}
}
eventType = xmlPullParser.next();
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
I/GetXmlActivity: --------------- name: jack
I/GetXmlActivity: --------------- age: 21
I/GetXmlActivity: --------------- name: tom
I/GetXmlActivity: --------------- age: 22
List users
集合中<users>
<user>
<name>jackname>
<age>21age>
user>
<user>
<name>tomname>
<age>22age>
user>
users>
当 xmlPullParser.getEventType() == XmlPullParser.START_TAG
以及 xmlPullParser.getName().equals("user")
时,我们已经到 user 开始标签
这个时候我们创建 User 对象
当 xmlPullParser.getEventType() == XmlPullParser.START_TAG
以及 xmlPullParser.getName().equals("name")
时,我们已经到 name 开始标签
这个时候我们再走一步,即可获取到 name 标签之间的文本内容并传递给 User 对象
当 xmlPullParser.getEventType() == XmlPullParser.START_TAG
以及 xmlPullParser.getName().equals("age")
时,我们已经到 name 开始标签
这个时候我们再走一步,即可获取到 age 标签之间的文本内容并传递给 User 对象
当 xmlPullParser.getEventType() == XmlPullParser.END_TAG
以及 xmlPullParser.getName().equals("user")
时,我们已经到 user 结束标签
这个时候我们把 User 对象放入 users 集合中
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
List<User> users = new ArrayList<>();
User user = null;
InputStream inputStream = getResources().openRawResource(R.raw.users);
XmlPullParser xmlPullParser = Xml.newPullParser();
try {
xmlPullParser.setInput(inputStream, null);
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = xmlPullParser.getName();
if (name.equals("user")) {
user = new User();
} else if (name.equals("name")) {
xmlPullParser.next();
String text = xmlPullParser.getText();
user.setName(text);
} else if (name.equals("age")) {
xmlPullParser.next();
String text = xmlPullParser.getText();
try {
user.setAge(Integer.parseInt(text));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
} else if (eventType == XmlPullParser.END_TAG) {
String name = xmlPullParser.getName();
if (name.equals("user")) {
users.add(user);
}
}
eventType = xmlPullParser.next();
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
for (User u : users) {
Log.i(TAG, "---------- " + u);
}
I/GetXmlActivity: ---------- User{name='jack', age=21}
I/GetXmlActivity: ---------- User{name='tom', age=22}