Introduction to XML

Getting started

Create a file with the XML file extension. Start your document by typing the xml root element.

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

Now you can create the structure of your XML document. This tutorial will make a simple news feed.

Creating a news item

For our news feed we will include a title, description and post date. These three pieces of information will form a news item. We will then use XML to store many of these modules.

<?xml version="1.0" encoding="UTF-8"?>
<mynews>
   <newsitem>
      <title>First News Post</title>
      <description>Description of first news post</description>
      <postdate>10/10/10</postdate>
   </newsitem>
</mynews>

Notice how this structure is very similar to HTML. We firstly created a newsitem element, and within this element added title, description and postdate elements.

All XML documents must have a root tag that encompasses all data, in this example mynews.
<?xml version="1.0" encoding="UTF-8"?>
<mynews>
   <newsitem>
      <title>First News Post</title>
      <description>Description of first news post</description>
      <postdate>10/10/10</postdate>
   </newsitem>
   <newsitem>
      <title>Second News Post</title>
      <description>Description of second news post</description>
      <postdate>12/10/10</postdate>
   </newsitem>
</mynews>

Now that we have defined a structure we can simply repeat the structure to create our document.

Character data

The example above shows simple text being entered into the description element, but what happens if you want to put characters that can be interpreted as XML e.g. "my <strong>bold</strong> text". This can be achieved in XML using CDATA sections.

<?xml version="1.0" encoding="UTF-8"?>
<mynews>

   <newsitem>
      <title>First News Post</title>
      <description><![CDATA[my <strong>bold</strong> text]]></description>
      <postdate>10/10/10</postdate>
   </newsitem>
</mynews>

Attributes

Attributes can be added in the same was as HTML.

<?xml version="1.0" encoding="UTF-8"?>
   <!-- Comment: news items must have a title, description and postdate -->
<mynews>
   <newsitem id="1">
      <title colour="blue">First News Post</title>
      <description>Description of first news post</description>
      <postdate type="d/m/y">10/10/10</postdate>
   </newsitem>
</mynews>

Comments

Comments can be inserted into XML documents to explain structure, leave reminders etc.

<?xml version="1.0" encoding="UTF-8"?>
   <!-- Comment: news items must have a title, description and postdate -->
<mynews>
   <newsitem>
      <title>First News Post</title>
      <description>Description of first news post</description>
      <postdate>10/10/10</postdate>
   </newsitem>
   <newsitem>
      <title>Second News Post</title>
      <description>Description of second news post</description>
      <postdate>12/10/10</postdate>
   </newsitem>
</mynews>

Note that because the comment delimiter is --> comments cannot end with a hyphen- e.g. <!--news---> is not valid but <!--news- --> is.

Categories

Tags

Social