homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

Can some help me in XML parsing using Java script

Msg#: 4277863 posted 4:47 pm on Mar 7, 2011 (gmt 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.




WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4277863 posted 5:18 pm on Mar 7, 2011 (gmt 0)

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

see: [msdn.microsoft.com...]


Msg#: 4277863 posted 11:54 pm on Mar 8, 2011 (gmt 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.


5+ Year Member

Msg#: 4277863 posted 9:01 am on Mar 22, 2011 (gmt 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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved