In this tutorial you will learn about android xml parsing using XMLPullParser.
在本教程中,您将学习使用XMLPullParser解析android xml的知识。
XML is a popular and very commonly used format for sharing data on internet. In android there are several ways to parse xml data like DOM, SAX and XMLPullParser. But in this article I will teach you how to parse xml using XMLPullParser.
XML是一种流行且非常常用的格式,用于在Internet上共享数据。 在android中,有几种解析xml数据的方法,例如DOM,SAX和XMLPullParser。 但是在本文中,我将教您如何使用XMLPullParser解析xml。
使用XMLPullParser进行Android XML解析 (Android XML Parsing Using XMLPullParser)
XML解析的步骤 (Steps for XML Parsing)
XML parsing includes following steps.
XML解析包括以下步骤。
1. First we have to read the xml file or data inside InputStream.
1.首先,我们必须读取InputStream中的xml文件或数据。
InputStream stream = this.getResources().openRawResource(R.raw.student);
Here I have assumed that student.xml file is present in res/raw folder of project. You can also fetch the xml data from internet using url. The xml file contains data of students as shown below.
在这里,我假设project.res / raw文件夹中存在student.xml文件。 您也可以使用url从Internet上获取xml数据。 xml文件包含学生数据,如下所示。
<?xml version="1.0"?>
<class>
<student>
<name>Neeraj Mishra</name>
<rollno>10</rollno>
<age>22</age>
</student>
<student>
<name>Mayank Khandiwal</name>
<rollno>11</rollno>
<age>22</age>
</student>
<student>
<name>Sumit Gendar</name>
<rollno>12</rollno>
<age>22</age>
</student>
<student>
<name>Vivek Pathak</name>
<rollno>13</rollno>
<age>22</age>
</student>
</class>
2. Create an