Skip to content

An introduction to XML

This section covers:

What is XML

  • XML stands for eXtensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to carry data, not to display data
  • XML is designed to be very descriptive
  • XML is a W3C Recommendation

Snippet of XML:

1
2
3
4
   <note to="Alice" from="Bob">
      <subject>Reminder</subject>
      <body>Don't forget to buy milk!</body>
   </note>

This example contains one parent element, a note element. This element is started with the opening note tag. It has meta information that is described through the use of attributes. This note element has two attributes: to and from. The note element has two child elements: subject and body. These elements don't have any attributes but do have some text content.

Tags

Like HTML, XML uses tags to open and close elements.

Unlike HTML, all tags in XML MUST be closed with either a closing tag:

1
   <subject>Reminder</subject>

Or self closing:

1
   <tag attribute="value"/>

Elements

An XML element is everything from (and including) the element's start tag to (and including) the elements end tag.

An element can contain:

  • Other elements (children)
  • Attributes
  • Text
  • Or a mix of all of the above...

Attributes

Attributes provide additional pieces of information about the information being stored in the XML.

The attributes in the note example above show the to and from recipients of the note, whereas the actual text of the subject and the body are child elements.

Entity references

XML has a couple of character limitations.

If you need to use any of the following characters within an element you must use the entity reference alternative:

Character/Symbol Entity Reference Description
< < Less than
& & Ampersand

For example if Alice's message to Bob was to 'get milk & bread', then it would be need to written as:

1
   <body>Don't forget to buy milk &amp; bread!</body>

Comments

Comments can be added into XML using the following syntax:

1
   <!-- Comments can be added into xml files using this syntax. -->

Note

Comments cannot include other comments (nested comments) nor can comments contain double dashes '--'.

More info

For more info on XML visit: W3Schools