XML Quick Tutorial

Zahid Ghadialy
May 2002

XML

 

XML was designed to describe data, and to focus on what data is. HTML was designed to display data, and to focus on how data looks.

 

What is XML?

        XML stands for EXtensible Markup Language

        XML is a markup language much like HTML

        XML was designed to describe data

        XML tags are not predefined in XML. You must define your own tags

        XML uses a Document Type Definition (DTD) or an XML Schema to describe the data

        XML with a DTD or XML Schema is designed to be self-descriptive

 

Lets start XML with one basic example.

 

Ex1.xml:

<?xml version="1.0"?>

<note>

<to>John</to>

<from>Joel</from>

<heading>Party</heading>

<body>Dont Forget the Party Tonight</body>

</note>

 

In the above example, the first line is the definition line saying that the above file is an XML file. If this line is omitted then this code will still work, but it is not a good idea to omit it.

 

The <note> is the root element. In XML it is necessary to have a closing tag without which the XML will not work unlike HTML. Also XML is case sensitive. If the starting tag is <to> and the ending tag is <To>, then this will not work. Also it is very important to have all the XML tags properly nested. There could be only one node element. There can be many children (sub elements).

 

We can have attributes along with the tags. For example we can modify the node element as follows:

<note date="31/10/2002">

 

Here is another example of XML.

 

Ex2.xml:

<?xml version="1.0"?>

<!-- This is just a comment -->

<book date="01/11/2002">

<title>My First XML</title>

<prod id="33-657" media="paper"></prod>

<chapter>Introduction to XML

<para>What is HTML</para>

<para>What is XML</para>

</chapter>

 

<chapter>XML Syntax

<para>Elements must have a closing tag</para>

<para>Elements must be properly nested</para>

</chapter>

 

</book>

 

XML elements must follow these naming rules:

 

        Names can contain letters, numbers, and other characters

        Names must not start with a number or punctuation character

        Names must not start with the letters xml (or XML or Xml ..)

        Names cannot contain spaces

 

Now lets see, what can be done with the XML documents. Lets make a food menu with the XML.

Ex3.xml:

<?xml version="1.0"?>

<?xml-stylesheet type="text/css" href="food_catalog.css"?>

<!-- Comment as usual -->

<breakfast_menu>

 

<food>

<name>Banana Pizza with Ice Cream</name>

<price>5.95</price>

<description>Some wiered description</description>

<calories>650</calories>

</food>

 

<food>

<name>Strawberry Belgian Waffles</name>

<price>7.95</price>

<description>light Belgian waffles covered with strawberries and whipped cream</description>

<calories>900</calories>

</food>

 

<food>

<name>Berry-Berry Belgian Waffles</name>

<price>8.95</price>

<description>light Belgian waffles covered with an assortment of fresh berries and whipped

cream</description>

<calories>900</calories>

</food>

 

<food>

<name>French Toast</name>

<price>"4.50</price>

<description>thick slices made from our homemade sourdough bread</description>

<calories>600</calories>

</food>

 

<food>

<name>Homestyle Breakfast</name>

<price>$6.95</price>

<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>

<calories>950</calories>

</food>

 

</breakfast_menu>

 

food_catalog.css:

food

{

Display: block;

background-color: #ffffff;

width: 100%;

}

 

name

{

color: #0000FF;

font-size: 20pt;

margin-bottom: 30pt;

margin-left: 0;

}

 

price

{

color: #FF0000;

}

 

description

{

Display: block;

font-size: 10pt;

}

 

calories

{

margin-right: 0;

color: #000000;

}

 

 

 

 





Back


HOME



About Us Careers Contribute Advertise






Copyright 2004-2013 3G4G.CO.UK. All rights reserved.
Contact zahidtg(at)yahoo(dot)com for further information