Using XML

Some of our online products, such as WordFind and MatchUp, use XML for their configuration.

XML is actually pretty easy to use, and if you've done any HTML it will seem very familiar.  This article is not meant to be an extensive discussion of XML (if you want one, try Googling "XML"), but it does cover the basics and in particular parts of XML relevant to our games.

XML nodes

An XML file is made up of nodes.  There is a single root node which in turn can contain other nodes, and those nodes in turn can contain more nodes and so on.

The snippet below shows a node which contains two other nodes.

<sounds>
<sound id="click" source="click.mp3"/>
<sound id="finish" source="finish.mp3"/>
</sounds>

Notice that the first tag <sounds> has a closing tag </sounds>.  In XML every node must be closed.

The <sound> nodes don't contain any other nodes so they can use the shorthand form of this.  You could write...

<sound id="click" source="click.mp3"></sound>

... but for empty nodes we make it shorter by putting the "/" before the closing brace, and removing the closing tag.  You will see a lot of these empty nodes in configuration files.

Tags

A node has a tag that tells us what type of node this is.

In the example, there is a node with the tag "sounds", and it contains two other nodes, both of which have the tag "sound".

Attributes

Each node can also have attributes, an attribute has a name, followed by an equals sign, and then a value in quotes.  Attributes must always be quoted (even if they a number).

Attributes are used extensively throughout our configuration files to define properties such as width and height, colors, captions and more.

It is also common for a node to have an "id" attribute.  The "id" may be used by the program to identify special elements, or be used when overriding the properties of an object defined in a template.

Child nodes

Nodes may contain other nodes.

In the above example, the <sounds> node represents a list of sounds.  It then contains separate <sound> nodes for each sound in the list.  The list is followed by a </sounds> closing tag.

Another common use of child nodes, especially in layout files, is to place one object inside another.  For example we might define a <frame> node that contains a <text> node and some <button> nodes.

Summary

  • XML is used for configuration files.
  • An XML file has a single root node.
  • Every node must be closed (empty nodes can use the short-hand form).
  • Tags define the type of node.
  • Attributes define properties.
  • Child-nodes represent items in a list or visual objects inside other visual objects.