Introduction to XML

XML is described in full in the W3C XML Specification but that is not something you want to read for fun. I recommend a great article Beginning XML - Chapter 2: Well-Formed XML on The Code Project, and here are some XML Links. The goal of firstobject is to skip to the essentials and not get embroiled in any details that will delay the adoption of XML in your software development.

A simple XML element is as follows:

<LOCATION>Alexandria</LOCATION>

The word LOCATION is the tag name. The start-tag is <LOCATION> and the end-tag is </LOCATION>. The Alexandria is the data content of the element. If an element is empty it may appear in either of two different ways. The second format uses a special empty-element tag rather than a start-tag and end-tag. You should not be concerned about which of these forms is used, the XML tool should treat both the same.

<LOCATION></LOCATION>
<LOCATION/>

The following element has an attribute named locid with the value 102. Attributes are specified inside the element's start tag.

<LOCATION locid="102">Alexandria</LOCATION>

An element may contain other elements between its start and end tags. The following example has a parent RECORD element containing a child NAME element and a child LOCATION element. The NAME and LOCATION elements are siblings of each other. Notice how the end tag of the RECORD is after the NAME and LOCATION.

<RECORD>
<NAME>John Smith</NAME>
<LOCATION locid="102">Alexandria</LOCATION>
</RECORD>

An XML document has one root element containing the whole document. In this example we'll call the root element LOCATIONLIST, and put a couple of RECORD elements inside it. See the LOCATIONLIST start tag and end tag encompassing the whole document.

<LOCATIONLIST>
<RECORD>
<NAME>John Smith</NAME>
<LOCATION locid="102">Alexandria</LOCATION>
</RECORD>
<RECORD>
<NAME>Jane Martin</NAME>
<LOCATION locid="104">New York</LOCATION>
</RECORD>
</LOCATIONLIST>

The contained elements are often shown indented to indicate that they are inside the containing element. The following XML is logically the same as the previous, it is just formatted differently.

<LOCATIONLIST>
  <RECORD>
    <NAME>John Smith</NAME>
    <LOCATION locid="102">Alexandria</LOCATION>
  </RECORD>
  <RECORD>
    <NAME>Jane Martin</NAME>
    <LOCATION locid="104">New York</LOCATION>
  </RECORD>
</LOCATIONLIST>

Parent elements must completely contain child element start and end tags or else the document is not well-formed. Both of the RECORD elements in the example have the same child elements, but there is no syntactical requirement that they have the same child elements. Schematic issues such as what child elements and attributes an element should have are up to the software developers who are implementing the data interchange.

This is all you need to know to start designing your own XML documents. If this is new to you, I do not recommend delving into more complex aspects of XML yet; see What Stops You From Using XML. To create and navigate a document like the one in this article with CMarkup see Example Shopping List.