Library tutorials & articles

Extensible Markup Language (XML) Tutorial

XML Elements

XML has a strict hierarchical syntax. The XML documents must contain a single root element, with child elements nested within the root element. Each element is defined with a start and close tag.

users.xml

<user>
    <name>
        <forename>Gez</forename>
        <surname>Lemon</surname>
    </name>
    <email>me@mystudio.com</email>
</user>

In the above example, "user" is the root element. The "name" and "email" elements are child elements of "user". The name element is further defined as having child elements "forname" and "surname".

The text between the tags is the content of the element.

Unlike HTML, whitespace is preserved with the contents of the elements. The following example will leave 5 spaces after the "forename" element.

<user>
    <name>
        <forename>Gez     </forename>
        <surname>Lemon</surname>
    </name>
    <email>me@mystudio.com</email>
</user>

If the content is converted to HTML, then the browser will ignore the white space. XML is case sensitive. Specifying a closing tag of /User in the above example would be incorrect.

Attributes of XML Elements

Like HTML, XML elements may have attributes in the start tag to provide extra information about that element. Attributes are name/value pairs. For example, the following might be the attributes for the <img> element in HTML.

<img src="images/logo.gif" alt="Logo" width="100" height="50" />

In XML, all attributes must be assigned values, and the values must be in quotes. The attribute name is case-sensitive. We could rewrite our previous example so that email was an attribute rather an element of the structure.

users.xml

<user email="me@mystudio.com">
    <name>
        <forename>Gez</forename>
        <surname>Lemon</surname>
    </name>
</user>

Attributes and child elements appear to achieve the same thing. However, attributes can be in any order and there may only be one instance of the attribute. If the attribute name is long it starts to get difficult to read, and attributes can't have a sub-structure. Elements can be ordered and the multiplicity can be defined. Elements may have a sub-structure and the length of the name is less important.

If order, structure or multiplicity are required, then an element should be used. One of the most common use of attributes in XML is where the information is not actually part of the data. For example, using an identifier for the "name" element.

users.xml

< user>
    <name nameid="64">
        <forename>Gez</forename>
        <surname>Lemon</surname>
    </name>
    <email>me@mystudio.com</email>
</user>

This enables a relationship to be defined from an external entity. The following example is a list of jobs that may be contracted. The constractor and placement are stored in a different data source, and linked using the nameid and placeid attributes respectively.

contract.xml

<contract>
    <job>
        <contractor nameid="64"/>
        <placement placeid="23"/>
        <rate>Free</rate>
    </job>
    ...
</contract>

Comments

  1. 19 Jan 2006 at 14:16

    Beutiful article. Takes a user from a beginner level to an intermediate level very nicely. Certainly a treat for beginers and an expert alike.

  2. 04 Apr 2004 at 22:28

    Good article for starters

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Extensible Markup Language (XML) Tutorial.

Leave a comment

Sign in or Join us (it's free).

Gez Lemon I'm available for contract work. Please visit Juicify for details.
AddThis

Related podcasts

  • LINQ to XML

    Scott's been poking around with LINQ to XML and reports his findings to Carl about life with XDocuments and XElements. They also talk about the bridge classes that link (no pun intended) System.Xml and System.Xml.Linq.

Events coming up

  • Jul 13

    SmartClient

    California, United States

    A Smart Client is an application that uses local processing, consumes XML Web Services and can be deployed and updated from a centralized server. While the .NET Framework (Windows Forms) and the .

Want to stay in touch with what's going on? Follow us on twitter!