Hi there

I have been trying to experiment with Parsing XML using Javascript. For some reason nothing happens. I found this example on W3 schools. Any idea why it's not working?

This is my HTML page which contains the Javascript

<html>
<head>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

x=xmlDoc.getElementsByTagName("CD");
i=0;

function displayCD()
{
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
</script>
</head>
<body onload="displayCD()">

<div id='showCD'></div>

</body>
</html>

This is my XML file saved in the same folder

<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>sdgdsgdg</title>
<artist>Breyrth</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
Member Avatar for iamthwee

Could be because the xml tags are case sensitive.

I tried the following with your xml and it worked...

<html>
<head>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","cd.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

x=xmlDoc.getElementsByTagName("cd");
i=0;

function displayCD()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;

}
</script>
</head>
<body onload="displayCD()">
<div>
<b>Title:</b> <span id="to"></span><br />
<b>Artist:</b> <span id="from"></span><br />
<b>Country:</b> <span id="message"></span>
</div>
<div id='showCD'></div>

</body>
</html>

Could be because the xml tags are case sensitive.

I tried the following with your xml and it worked...

<html>
<head>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","cd.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

x=xmlDoc.getElementsByTagName("cd");
i=0;

function displayCD()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;

}
</script>
</head>
<body onload="displayCD()">
<div>
<b>Title:</b> <span id="to"></span><br />
<b>Artist:</b> <span id="from"></span><br />
<b>Country:</b> <span id="message"></span>
</div>
<div id='showCD'></div>

</body>
</html>

Thank you so much for taking time out to help a newbie. Please post the cd.xml file that you used. I'm getting a bit confused.

Regards

Member Avatar for iamthwee

I used the same xml file as you. I just renamed it to cd.xml

I used the same xml file as you. I just renamed it to cd.xml

I've just realised. It works in Firefox but not in chrome and internet explorer :-(

Thank you so much for your help though.

Member Avatar for iamthwee

Yeah I just realised that too...

Something must be wrong with the code because it should work for all browsers, let me check.

I've figured out what it is. Apparently Chrome and IE have issues with certain scripts running on local machines. I ran IT in from my localhost instead and it worked on all browsers. Thank you so much for your help. It's people like you who take time out to help others that makes the difference. THANK YOU !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.