XML Basics

This page will explain the basics of XML.

XML Syntax

Example of an XML document:

<?xml version="1.0" encoding="ISO-8859-1"?>

<factory>
	<contactinfo>
		<person>Mike S.</person>
		<street>somestreet</street>
		<country>Belgium</country>
		<website>www.xeml.net</website>
	</contactinfo>
	<slogan>You ask, we do</slogan>
</factory>
XML Declaration

A declaration should always be included on top of your XML document. It defines the XML version of your document. We choose for the 1.0 specification of XML and the ISO-8859-1 (Latin-1/West European) character set.

The XML Declaration should normally be written like this:

<?xml version="versionNumber" encoding="encodingDeclaration" standalone="standaloneStatus" ?>

Most common version attribute value(s):

version="1.0"
version="1.1"

Most common encoding attribute value(s):

encoding="UTF-8"
encoding="UTF-16"
encoding="ISO-10646-UCS-2"
encoding="ISO-10646-UCS-4"
encoding="ISO-8859-1" (to ISO-8859-9)
encoding="ISO-2022-JP"
encoding="Shift_JIS"
encoding="EUC-JP"

Standalone attribute value(s):

no
yes

Include the attribute standalone="no" if any elements, attributes, or entities are used in the XML document that are referenced or defined in an external DTD.

Root element
<factory>
	...
</factory>

The next line after the XML declaration is the XML root element. It describes the whole document and all other child elements must be placed between the root element. In our case the document will describe a factory. Note: there can only be one root element!

Tag

A tag starts with a less-than character (<) and ends with a great-than character (>).

There are two kinds of tags: an start-tag and an end-tag. Those two tags define an element The end-tags have a slash (/) after the less-than character.

This example show the start- and end-tag:

<mytag></mytag>

The tags below are all similar:

<mytag></mytag>
<mytag />
<mytag/>
Element

An element consists of a start-tag, followed with text and/or child elements and should be closed with the end-tag. XML elements and attributes are case sensitive!

Example of an element:

<mytag>text <childtag>other text in the child tag</childtag> other text</mytag>

XHTML element:

<p><em>text</em> <strong>other text in the child tag</strong> other text</p>
Namespaces

Many programming languages (CSharp, Jsharp, PHP,...) have the possibilities to define namespaces. XML can also define namespaces.

Why should I use namespaces? - The namespace reffers to the place where elements can be used and to prevent collision with other elements that have the same name but are used in another file/part/section/...

xmlns = XML-namespace.

<?xml version="1.0" encoding=utf-8"?>

<factory xmlns="http://somePlace/factory">
	<contactinfo>
		<person>Mike S.</person>
		<street>somestreet</street>
		<country>Belgium</country>
		<website>www.xeml.net</website>
	</contactinfo>
	<slogan>You ask, we do</slogan>
</factory>
Attributes

In the example below we will use our first xml example document and tweak it a little bit. The result should be clear enough to understand xml attributes.

<?xml version="1.0" encoding="ISO-8859-1"?>

<factory>
	<contactinfo country="1">
		<person>Mike S.</person>
		<street>somestreet</street>
		<website>www.xeml.net</website>
	</contactinfo>
	<slogan>You ask, we do</slogan>
</factory>

We just used our - <country>Belgium</country> - element as an attribute now.

Use an element if you want more structure and layout possibilities or you want to manipulate them. Use an attribute for metadata (info), to describe your describing data. If you don't know what to choose, use an element.