We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,459 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with Ajax XML parsing

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>
2
Contributors
7
Replies
4 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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>
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

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

Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

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.

Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Yeah I just realised that too...

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

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

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

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

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

Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0833 seconds using 2.71MB