Sample code sharing of the loop process when using PULL to parse XML files

黄舟
Release: 2017-03-18 16:40:53
Original
1794 people have browsed it

XMLThe content of the file is as follows (the name is "teacher.xml") zhangsan 1912000 lisi 23 8000 Use PULL to parse XML File code:

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class PullTry {
public static void main(String[] args) {
List datas = null;
Teacher teacher = null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new FileReader("teacher.xml"));
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
// 创建list
datas = new ArrayList<>();
break;
case XmlPullParser.START_TAG:
// 创建Teacher对象
if ("teacher".equals(tagName)) {
teacher = new Teacher();
} else if ("name".equals(tagName)) {
teacher.setName(parser.nextText());
} else if ("age".equals(tagName)) {
teacher.setAge(Integer.parseInt(parser.nextText()));
} else if ("money".equals(tagName)) {
teacher.setMoney(Double.parseDouble(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
// 把对象添加进List集合中
if ("teacher".equals(tagName)) {
datas.add(teacher);
}
break;
default:
break;
}
// 将eventType指向下一步...
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < datas.size(); i++) {
System.out.println(datas.get(i));
}

System.out.println(count);
}
}
Copy after login

Here we mainly look at while loopHow to read the xml file internally..

The first loop condition is eventType != XmlPullParser.END_DOCUMENT

If eventType is not equal to XmlPullParser.END_DOCUMENT, proceed to the next cycle. There are 11 eventType values ​​here, but only the first 5 are used.

public static final int START_DOCUMENT = 0;
public static final int END_DOCUMENT = 1;
public static final int START_TAG = 2;
public static final int END_TAG = 3;
public static final int TEXT = 4;
Copy after login

In order to observe the process of loop execution, I added a line of code inside the loop:

System.out.println(eventType+"===第"+count+"次循环==="+tagName);
Copy after login

where count is the number of times the loop runs. After adding this line of code, the console displays the following information:

0===第1次循环===null 
2===第2次循环===teachers 
4===第3次循环===null 
2===第4次循环===teacher 
4===第5次循环===null 
2===第6次循环===name 
4===第7次循环===null 
2===第8次循环===age 
4===第9次循环===null 
2===第10次循环===money 
4===第11次循环===null 
3===第12次循环===teacher 
4===第13次循环===null 
2===第14次循环===teacher 
4===第15次循环===null 
2===第16次循环===name 
4===第17次循环===null 
2===第18次循环===age 
4===第19次循环===null 
2===第20次循环===money 
4===第21次循环===null 
3===第22次循环===teacher 
4===第23次循环===null 
3===第24次循环===teachers Teacher [name=zhangsan, age=19, money=12000.0] Teacher [name=lisi, age=23, money=8000.0] 24
Copy after login

First of all, only when the next eventType value is 4 (that is, TEXT is read in the next loop), the content read parser.nextText() is not empty

Combined with the xml file, we can draw the conclusion:

In the first cycle, the eventType value is 0, which is the case of START_DOCUMENT. At this time the tagName value is empty.

In the second cycle, the eventType value is 2, which is the case of START_TAG. At this time, the tagName value is teachers. In other words, the tag is read at this time. Since the next eventType value is 4, which is TEXT, the parser.nextText() value of the content read this time is the text content from the label to the middle of the label! That is, a /n plus a /t.

The third cycle, the eventType value is 4, which is the case of TEXT. At this time the tagName value is empty.

In the fourth cycle, the eventType value is 2, which is the case of START_TAG. At this time, the tagName value is teacher. In other words, the tag is read at this time. Note: At this time, the if statement determines that the return value of "teacher".equals(tagName) is true, and the following teacher = new Teacher(); will be executed to create an object of the Teacher class. Since the next eventType value is 4, which is TEXT, the parser.nextText() value of the content read this time is the text content from the label to the middle of the label, that is, one /n plus two /t.

In the fifth cycle, the eventType value is 4, which is the case of TEXT. At this time the tagName value is empty.

In the sixth cycle, the eventType value is 2, which is the case of START_TAG. At this time, the tagName value is name. In other words, the tag is read at this time. Note: At this time, the if statement determines that the return value of "name".equals(tagName) is true, and the following teacher.setName(parser.nextText()); will be executed. The value of parser.nextText() here should be zhangsan. In this way, zhangsan is assigned to the name attribute of the teacher object.

In the seventh cycle, the eventType value is 4, which is the case of TEXT. At this time the tagName value is empty.

It seems that there is no need to say anything later, it all means the same thing.

ps: Every time a Teacher tag is encountered (Teacher tag is a child tag, Teachers is a parent tag), an object will be created, and then the grandchild tags (name, age and money) is assigned to the properties of this object.

ps2: In this example, the endtag in the grandchild tag will not be read. (That's it...)

xbox: There are only three cases where the eventType value is 3. In these three cases, the tagName values ​​are teacher, teacher and teachers respectively. It further proves that ps2 is right....

The above is the detailed content of Sample code sharing of the loop process when using PULL to parse XML files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!