XML Dissected

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

xml document: dissection

Excuse the unpleasant title of this XML lesson, but the name is suited for what we are about to do. In this lesson, we will be taking apart and analyzing each part of an XML document. Consider this an overview of the XML document, and do not be unsettled if you don't understand all that is discussed in this lesson. We will cover all these parts in greater detail throughout the tutorial.

parts of an xml document

An XML document is made up of a small group of building blocks. W3C wanted to keep XML simple, and judging by the relatively few components of an XML document, they seem to have succeeded. We have divided the components into sections and subsections.

  • Prolog

    • XML Declaration

    • DTD Declaration

  • Root Element (Document)

    • Elements (Nested Elements)

      • Element Attributes and Values

      • Data

Webpage standards now recommend that an HTML document follow this form, so that they can be considered valid XML. Fun Fact: In an HTML file the root element is


xml prolog

The XML prolog is an optional piece of information that must come before the root element when used. There are two pieces that make up the prolog: the XML declaration and the Document Type Declaration (DTD).

The XML declaration states which version of XML you are using, while the DTD describes the rules your XML document must follow. If you are just dying to know more about the DTD and can't wait to read through the lessons as we have laid them out (we won't be offended), check out our XML DTD lesson.

xml root element (document)

The root element, also referred to as the "document", dictates what kind of XML document it is. When creating an HTML file that is XML complaint, the root element will be

. The root element must be the first element in an XML document and there can only be one root element per file!

xml element & attributes

The meat of an XML document comes from the elements that are contained within the root element. Each element represents a different type of data that is being stored in the document. The element

might represent paragraph text, while the element may contain data for a GIF image.

Although not required, elements often have attributes that are associated with them. XML attributes are similar to HTML attributes in that they have a name:value relationship. An example attribute for an element might be "src", which represents the source location of the image.

XML Element & Attribute Code:

<img src = "C:/MyDocs/Happy.gif"></img>

xml data

In addition to the information stored in the element itself (attributes), the bulk of data in XML usually appears between the opening and closing tag of an XML element. This is often referred to as the XML's "content". Below, we have an imaginary XML document that stores a story.

XML Code:

	<author>Jill Doe</author>
	<title>The Truth About the Family</title>
		<paragraph>Here's a story of a lovely lady who...</paragraph>
		<paragraph>...they became the Brady Bunch...</paragraph>

Quick Learning Check:

Can you tell which element is the root element in the above XML code? If you thought , then you were right on the money!