


Sample code sharing of the loop process when using PULL to parse XML files
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); } }
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;
In order to observe the process of loop execution, I added a line of code inside the loop:
System.out.println(eventType+"===第"+count+"次循环==="+tagName);
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
