10.1  XML Crash Course

by Martin Grimme

10.1.1  Introduction

This tutorial shows you the basics of the eXtensible Markup Language XML, which is an essential part of gDesklets.

10.1.2  It's about Trees

Think of an XML document as a tree of genealogy: there are ancestors, children, siblings, and so on. These individuals are known as nodes. Every XML document requires a root node, which is the ancestor of all other nodes.

Nodes are represented by pointy braces between which you can find their name. This representation is called a tag. Here you can see some examples of tags:

<Kronos>
<Zeus>
<Artemis>
<Leda>
  

Their relationship is as follows: Kronos is the father of Zeus, who in turn is the father of Artemis and Leda, who are siblings.

This genealogy with Kronos as the root node can be represented as follows:

<Kronos>
  <Zeus>
    <Artemis/>
    <Leda/>
  </Zeus>
</Kronos>
  

The indentation is only there to emphasize the hierarchical structure, it is not needed but strongly recommended to increase readability.

Whenever a node has children, then it consists of an opening and a closing tag. The closing tag has to contain a slash (/) as its first character. The children are simply put between the opening and closing tags.

For nodes without children, the opening and closing tags are merged into a single tag where the slash is the last character (immediately before the closing brace).

That way you can describe hierarchical structures easily using plain text files. The information is well-structured and thus apt for being read by machines as well as by humans.

10.1.3  Attributes

Tags may also have a number of attributes describing the node. Zeus, e.g. lives on the Olymp and may thus have an attribute place telling us about where he lives:

<Kronos>
  <Zeus place="Olymp">
    <Artemis/>
    <Leda/>
  </Zeus>
</Kronos>
  

10.1.4  Help the Parser

You should help the XML parser by putting the following before the root node as the first line in XML documents:

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

This specifies that the following document complies with the 1.0 version of the XML specification and that text is encoded in UTF-8 unicode, the standard of GNOME (please make sure that the text encoding really is UTF-8 then).