XML is a very important base on which Web Services work, and, in conjunction with a number of client- and server-side languages, can be put to good effect.
I’ll use IE6 to explain the codes. Browsers that don’t support XML can’t read these, so when you view an XML file in such a browser, it will simply ignore all the tags.
Sample XML File
Let’s consider a sample XML file, which shows employee data and Turnover of a company:
<?xml version="1.0" ?>
<employee id="001" sex="M" age="19">Premshree Pillai</employee>
<employee id="002" sex="M" age="24">Kumar Singh</employee>
<employee id="003" sex="M" age="21">Ranjit Kapoor</employee>
Load The XML File
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
// 0 Object is not initialized
// 1 Loading object is loading data
// 2 Loaded object has loaded data
// 3 Data from object can be worked with
// 4 Object completely initialized
if (xmlDoc.readyState != 4)
Now the XML file can be loaded:
Display The Contents of the XML File
View the entire contents of the XML file using
alert(xmlObj.xml); The whole XML file will be displayed in an alert box as it is, with proper indentation.
Children and Nodes
In the above XML file,
<company> is the top level tag under which all other tags fall. These tags are called children. This XML file can be represented graphically like a folder-tree:
In the above XML file, the top level tag
<company> has 4 children.
The numbering of children (as is usual in all languages) starts from 0 (zero). The
<turnover> tag has 3 children under it.
We can find the number of children a tag has by using the
childNodes.length property. Thus the number of children of
<company> tag (here, 4) can be found by using
The number of children of
<turnover> tag (here, 3) can be found by using
Here we use
<turnover> is the 3rd child of
Test for Children
You can test whether a particular node child has any children using
xmlObj.childNodes(3).hasChildNodes() will return true.
xmlObj.childNodes(2).hasChildNodes() will return false, as the
<employee> tag doesn’t have any children.
Get Tag Name
You can get the tag name of a child using
xmlObj.tagName will return "company".
xmlObj.childNodes(0).tagName will return "employee".
xmlObj.childNodes(3).childNodes(0).tagName will return "year".
Display the Content of a Tag
In the XML file, the content of the 1st
<employee> tag is "Premshree Pillai". You can get this value using
xmlObj.childNodes(2).firstChild.textwill return "Suhasini Pandita". Similarly,
xmlObj.childNodes(3).childNodes(1).firstChild.textwill return "140,000".
In the XML file, the
<employee> tag has 3 attributes. An attribute can be accessed using
xmlObj.childNodes(0).getAttribute("id") will return "001".
xmlObj.childNodes(1).getAttribute("age") will return "24". And
xmlObj.childNodes(2).getAttribute("sex") will return "F".
Premshree studies engineering in Information Technology at Mumbai University, India. He's written articles for a range of popular Indian IT magazines, and his site, QikSearch has featured in 'Digit' Magazine - India's No. 1 technology magazine.