Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Can some help me in XML parsing using Java script

4:47 pm on Mar 7, 2011 (gmt 0)

New User

5+ Year Member

joined:Mar 7, 2011
votes: 0


I am trying to prase an XML file using java script.

Here is file: employee.xml

< ?xml version="1.0" encoding="UTF-8" ?>
<employee id="001" >John</employee>
<year id="2000">100,000</year>
<year id="2001">140,000</year>
<year id="2002">200,000</year>

Here is script which I saved as parser.htm

<title>Read XML in Microsoft Browsers</title>
<script type="text/javascript">
var xmlDoc;
function loadxml()
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.onreadystatechange = readXML;

function readXML()
if(xmlDoc.readyState == 4)
//Using documentElement Properties
//Output company
alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);

//Using firstChild Properties
//Output year
alert("First Child: " + xmlDoc.documentElement.childNodes[1].firstChild.tagName);

//Using lastChild Properties
//Output average
alert("Last Child: " + xmlDoc.documentElement.childNodes[1].lastChild.tagName);

//Using nodeValue and Attributes Properties
//Here both the statement will return you the same result
//Output 001
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue);
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes.getNamedItem("id").nodeValue);

//Using getElementByTagName Properties
//Here both the statement will return you the same result
//Output 2000
alert("getElementsByTagName: " + xmlDoc.getElementsByTagName("year")[0].attributes.getNamedItem("id").nodeValue);

//Using text Properties
//Output John
alert("Text Content for Employee Tag: " + xmlDoc.documentElement.childNodes[0].text);

//Using hasChildNodes Properties
//Output True
alert("Checking Child Nodes: " + xmlDoc.documentElement.childNodes[0].hasChildNodes);


<body onload="loadxml();">


I can not see any output. Can some help me.

5:18 pm on Mar 7, 2011 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
votes: 0

Have you tried using IE's debugging tools to see what the error is?

see: [msdn.microsoft.com...]
11:54 pm on Mar 8, 2011 (gmt 0)

New User

5+ Year Member

joined:Feb 13, 2011
posts: 8
votes: 0

I won't look into the code itself - as mentioned: using a debugger is key (Suite vel morimini ;) ).

Make sure you run the code of a webserver - otherwise I wouldn't expect that to work.
9:01 am on Mar 22, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
votes: 0

Hmmm... sneaky little errors can and do often cause lots of troubles. And such is the case here.
First thing is the xml file opening xml declaration. Should not be a blank space between the opening < and ?
you have < ?xml when it should be <?xml
Second thing is that you are missing a closing brace } for the "if" statement in your readXML function.

The code will work locally in IE if you tell IE to allow it by clicking the yellow bar and then "Allow blocked content"

Oh, wow I must be zonked, just realized this is two weeks old. Oh well, maybe it'll help somebody