What is XML? Using XML to Parse Tree Structure Data


In real life, there is a certain relationship between many things. For example, there are many provinces in China, and there are many cities under each province. The relationship between these provinces and cities can be described by a tree structure diagram, as shown in Figure 1.

What is XML? Using XML to Parse Tree Structure Data

Figure 1 City relationship map

Figure 1 intuitively describes the hierarchical relationship between China and the provinces and cities under its jurisdiction. But for the program, it is very difficult to parse the content of the picture. At this time, it is the best choice to use XML files to save the data with tree structure.

XML is the abbreviation of EXtensible Markup Language, which is a markup language similar to HTML, called Extensible Markup Language. The so-called extensibility means that users can customize tags according to XML rules.

Next, describe the relationship shown in Figure 1 through an XML document, as shown in Example 1.

Example #1 city.xml

< China >
     < Hebei >
        < City > Zhangjiakou </ city >
        < City > Shijiazhuang </ city >
     </ Hebei >
      < Shanxi >
        < City > Taiyuan </ city >
        < City > Datong </ city >
     </ Shanxi >
</ China >

In Example 1, < China >, < Hebei >, and < city > are user-created tags that can be called elements. These elements must come in pairs, that is, include a start tag and an end tag. For example, in the < China > element, the start tag is < China > and the end tag is </ China >. < China > is considered the root element of the entire xml document, and below it are two child elements, Hebei and Shanxi, which in turn contain two < city > elements. In XML documents, complex information with tree-like hierarchy can be accurately described through the nested relationship of elements. Therefore, more and more applications are using XML format to store relevant configuration information for easy reading and modification.