XML Tree


XML documents form a tree structure that starts at “the root” and branches to “the leaves“. XML documents are formed as element trees. An XML tree starts at a root element and branches from the root to child elements. All elements can have sub elements (child elements).

The terms parent, child, and sibling are used to describe the relationships between elements. Parent have children. Children have parents. Siblings are children on the same level (brothers and sisters). All elements can have text content and attributes (category=”cooking”).

Below is just an example of the hierarchical structure of XML. There is another post on this website called XML Attributes vs. Elements that shows the difference between content and attributes. Those of you familiar with HTML will be familiar with there differences already. The code below does not show content in attributes or elements.

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

A prolog defines the XML version and the character encoding, as shown below. Prologs are optional in XML documents and if it is included, it must be the first line. UTF-8 is the default character encoding for XML documents.

<?xml version="1.0" encoding="UTF-8"?>

Using an example at w3schools.com, the next line is the root element of the document. An XML element is everything from (including) the element’s start tag to (including) the element’s end tag. An element can contain: text, attributes, other elements, or a mix of the above. Elements can be empty.

<bookstore>

The next line starts a element. XML elements can have attributes, just like HTML. Attributes are designed to contain data related to a specific element. In this example, category is an attribute. Cooking is an attribute value. Attribute values must always be quoted. Either single or double quotes can be used. So below we have an element, an attribute and an attribute value.

<book category="cooking">
Series Navigation<< XML Attributes vs. ElementsXML DOM >>

Leave a comment

Your email address will not be published. Required fields are marked *