XML - structure

Structure of an XML document

An XML document consists of two main parts: the Prolog and the Root element (or document element).


Prolog

The prolog is a header section, consisting of the XML declaration and programmer’s comments.

The first line is the XML declaration, which states that this is an XML document and gives the version number. The XML declaration is optional, although the specification states that it should be included. If an XML declaration is included, it must appear at the beginning of the document.

The syntax of a XML declaration is:

<?xml version=version_number>

The prolog may also include comments, which are ignored by the XML processor. Comments are optional in an XML document, but can aid its readability. A comment takes the form:

<!– comment text>

The comment text may include any characters except “–” (two successive hyphens).


Root element

An XML document has one top-level element, known as the root (or document) element. This element is the second main part of the document. It can contain additional elements. The elements indicate the logical structure of the document and contain the document’s information content. There cannot be more than one top-level element.

The content of an element is enclosed between a start tag and an end tag:

<element_name>
     element content
</element_name>

The element_name is free text, and normally indicates the nature of the information in the element. The name in the start tag must match the name in the corresponding end tag exactly. Element names are case-sensitive.

The element content can be character data, other (nested) elements, or a combination of both. Elements must be properly nested.

It is important that the document’s syntax conforms to the minimal set of rules described above, if it is to be correctly processed by a browser or other program. An XML file that contains errors cannot be used.

See XML elements for more information.


EXAMPLE

In this example the root element is named INVENTORY. It contains two BOOK elements, each of which contains four further elements. Note the nesting structure:

<INVENTORY>
  <BOOK>
    <TITLE>XML Step by Step</TITLE>
    <AUTHOR>Michael J. Young</AUTHOR>
    <PAGES>382</PAGES>
    <PRICE>$49.99</PRICE>
  </BOOK>
  <BOOK>
    <TITLE>XML Step by Step</TITLE>
    <AUTHOR>David Hunter</AUTHOR>
    <PAGES>823</PAGES>
    <PRICE>$39.99</PRICE>
  </BOOK>
</INVENTORY>

RELATED TOPICS

XML