Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML tree structure


May 27, 2021 XML


Table of contents


XML documents form a tree structure that starts at the "root" and then extends to the "branches."

A tree structure is often referred to as an XML tree and can easily describe any XML document.

By adopting a tree structure, you can know all subsequent branches and branches starting at the root.


An instance of an XML document

XML documents use simple, self-descriptive syntax:

<?xml version="1.0" encoding="ISO-8859-1"?> <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 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 note"):

<note>

The next 4 lines describe the 4 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 assume that from this example, the XML document contains a note that Jani wrote to Tove.

XML is excellently self-descriptive, do you agree?


XML documents form a tree structure

The XML document must contain the root element. The element is the parent of all other elements.

Elements in an XML document form a document tree. The 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 such as parent, child, and sibling are used to describe the relationship between elements. T he parent element has child elements. Child elements at the same level become compatriots (brothers or sisters).

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


Instance:

XML tree structure

The image above represents a book in XML below:

<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 >Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>

The root element in the instance is the .lt;bookstore. All of the elements in the document are included in the .

There are 4 sub-elements of the element: slt;title,

In the following section, we'll cover the syntax of XML.