Home  >  Article  >  Backend Development  >  XML development basics-XML tree structure

XML development basics-XML tree structure

黄舟
黄舟Original
2017-03-25 17:21:171870browse

XMLThe document forms a tree structure, starting from the "root" and extending to the "leaves".

An XML document example

XML uses a simple self-descriptive syntax:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<note> <to>George</to> <from>John</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget the meeting this weekend!</body> 
</note>

The first line is the XML declaration. It defines the version of XML (1.0) and the encoding used (ISO-8859-1 = Latin-1/Western European Character Set).

The next line describes the root element of the document (like saying: "This document is a sticky note"):

<note>

The next 4 lines describe the 4 child elements of the root (to, from, heading and body):

<to>George</to> <from>John</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget the meeting this weekend!</body>

The last line defines the end of the root element:

</note>

As you can imagine from this example, the XML document contains a note from John to George.

XML is brilliantly self-describing, don’t you agree?

XML documents form a tree structure

XML documents must contain a root element. This element is the parent element of all other elements.

The elements in the XML document form a document tree. The tree starts at the root and expands to the very bottom of the tree.

All elements can have child elements:

<root> <child> <subchild>.....</subchild> </child> </root>

Terms such as parent, child, and sibling are used to describe the relationship between elements. Parent elements own child elements. Child elements at the same level become siblings (brothers or sisters).

All elements can have text content and attributes (similar to HTML).

Example

XML development basics-XML tree structure

The above figure represents a book in the following XML:

<bookstore> 
<book category="COOKING"> 
<title>Everyday Italian</title> 
<author>Giada De Laurentiis</author> 
<year>2005</year> <price>30.00</price> 
</book> 
<book category="CHILDREN"> 
<title>Harry Potter</title> 
<author>J K. Rowling</author> 
<year>2005</year> 
<price>29.99</price> </book> 
<book category="WEB"> 
<title>LearningXML</title> 
<author>Erik T. Ray</author> 
<year>2003</year> 
<price>39.95</price> 
</book> 
</bookstore>

The root element in the example is elements in the document are contained within a .

The element has 4 child elements: , <author>, <year>, <price>. </price></year></author>

The above is the detailed content of XML development basics-XML tree structure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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