XML Introduction

XML is not a replacement for HTML.
XML and HTML were designed with different goals:

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.

HTML is about displaying information, while XML is about describing information.
The Extensible Markup Language (XML) became a W3C Recommendation 10. February 1998.

What is XML?


How can XML be Used?

It is important to understand that XML was designed to store, carry, and exchange data. XML was not designed to display data.
*XML can Separate Data from HTML.
- With XML, your data is stored outside your HTML.
*XML is Used to Exchange Data
- With XML, financial information can be exchanged over the Internet.
- Expect to see a lot about XML and B2B (Business To Business) in the near future.
*With XML, data can be exchanged between incompatible systems.
*XML Can be Used to Create New Languages
- XML is the mother of WAP and WML.
- The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like mobile phones, is written in XML.

Syntax Rules

The syntax rules of XML are very simple and very strict. The rules are very easy to learn, and very easy to use. Because of this, creating software that can read and manipulate XML is very easy.
The first line of the XML document:
<?xml version="1.0" encoding="ISO-8859-1"?>

The first line in the document - the XML declaration - defines the XML version and the character encoding used in the document. In this case the document conforms to the 1.0 specification of XML and uses the ISO-8859-1 (Latin-1/West European) character set.

<?xml version="1.0" encoding="ISO-8859-1"?> - the XML declaration
<note> - describes the root element of the document (like it was saying: "this document is a note")
<to>Tove</to> - next 4 lines describe 4 child elements of the root 
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> - defines the end of the root element

XML Documents Must Have a Root Element

All XML documents must contain a single tag pair to define a root element.
<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root> 

Displaying XML with XSL

XSL - eXtensible Stylesheet Language
- is far more sophisticated than CSS. One way to use XSL is to transform XML into HTML before it is displayed by the browser

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>

    <description>
       two of our famous Belgian Waffles
    </description>
    <calories>650</calories>
  </food>
</breakfast_menu>

If you want to learn more about XSL, please visit our XSL tutorial.


XML Data Island

With Internet Explorer, the unofficial <xml> tag can be used to create an XML data island. An XML data island is XML data embedded into an HTML page.


Bind Data Island to HTML Elements

In the next example, we will embed an XML file called "cd_catalog.xml" into an HTML file.

View "cd_catalog.xml".

The HTML file looks like this:

<html>
<body>

<xml id="cdcat" src="cd_catalog.xml"></xml>

<table border="1" datasrc="#cdcat">

<tr>
<td><span datafld="ARTIST"></span></td>
<td><span datafld="TITLE"></span></td>
</tr>
</table>

</body>
</html>

Example explained:

The datasrc attribute of the <table> tag binds the HTML table element to the XML data island. The datasrc attribute refers to the id attribute of the data island.

<td> tags cannot be bound to data, so we are using <span> tags. The <span> tag allows the datafld attribute to refer to the XML element to be displayed. In this case, it is datafld="ARTIST" for the <ARTIST> element and datafld="TITLE" for the <TITLE> element in the XML file. As the XML is read, additional rows are created for each <CD> element.


XML in Real Life

A real-life example of how XML can be used to carry information.


Example: XML News

XMLNews is a specification for exchanging news and other information.

Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.

An example XMLNews document:

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

<nitf>

<head>

<title>Colombia Earthquake</title>
</head>

<body>

<headline>
  <hl1>143 Dead in Colombia Earthquake</hl1>

</headline>
<byline>
  <bytag>By Jared Kotler, Associated Press Writer</bytag>
</byline>
<dateline>
  <location>Bogota, Colombia</location>

  <date>Monday January 25 1999 7:28 ET</date>
</dateline>

</body>

</nitf>


XML Parser

Parsing XML Documents

To read and update, create and manipulate an XML document, you will need an XML parser.

To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree.

To learn more about the XML DOM, please read our XML DOM tutorial.

There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. In this tutorial we will show you how to create cross browser scripts that will work in both Internet Explorer and Mozilla browsers.

Read more about parsing in XML_Parser