1,105,386 Community Members

Help with Ajax XML parsing

Member Avatar
Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
iamthwee
Posting Sage
7,038 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 592 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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>
Member Avatar
Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
iamthwee
Posting Sage
7,038 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 592 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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

Member Avatar
Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
iamthwee
Posting Sage
7,038 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 592 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Yeah I just realised that too...

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

Member Avatar
iamthwee
Posting Sage
7,038 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 592 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Not too sure what it is, but a quick search recommends using JQuery to do this...

http://stackoverflow.com/questions/649614/xml-parsing-in-javascript

Member Avatar
Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 !!!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: