Welcome to WebmasterWorld Guest from 54.145.166.96

Forum Moderators: httpwebwitch

Message Too Old, No Replies

Can some help me in XML parsing using Java script

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



Hello.

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

Here is file: employee.xml

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


Here is script which I saved as parser.htm

<html>
<head>
<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;
xmlDoc.load("employee.xml");
}

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);
}

</script>
</head>

<body onload="loadxml();">

</body>
</html>

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

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

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



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)



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)

5+ Year Member



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month