XML Tree Structure
XML documents form a tree structure that starts at the "root" and extends to the "leaves."
An XML Document Example
XML documents use a simple, self-descriptive syntax:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (UTF-8: Unicode, capable of displaying various languages).
The next line describes the root element (as if saying, "This document is a note"):
<note>
The following four lines describe the four child elements of the root (to, from, heading, and body):
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
The last line defines the end of the root element:
</note>
You can infer from this example that the XML document contains a note from Jani to Tove.
XML boasts excellent self-descriptiveness, don't you agree?
XML Documents Form a Tree Structure
An XML document must contain a root element. This element is the parent of all other elements.
Elements in an XML document form a document tree. This tree starts at the root and extends to the bottom of the tree.
All elements can have child elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
Terms like parent, child, and sibling are used to describe the relationships between elements. A parent element has child elements. Child elements at the same level are siblings (brothers or sisters).
All elements can have text content and attributes (similar to HTML).
Example:
The diagram represents a book from the following XML:
XML Document Example
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
The root element in the example is <bookstore>. All <book> elements are contained within <bookstore>.
The <book> element has four child elements: <title>, <author>, <year>, <price>.