Jump to content

Javascript/xml Problem


jin_pengyou
 Share

Recommended Posts

Im trying to load XML content using Javascript. I used the code from the Tutorial on W3schools.com. Everything is working in Firefox but not in IE.Here's the code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Untitled Document</title><link href="style.css" rel="stylesheet" type="text/css" title="Style Gris"><script type="text/javascript" language="javascript"><!--if (window.XMLHttpRequest)  {  xmlDoc=new window.XMLHttpRequest();  xmlDoc.open("GET","articles/article1.xml",false);  xmlDoc.send("");  xmlDoc=xmlDoc.responseXML;  }// IE 5 and IE 6else if (ActiveXObject("Microsoft.XMLDOM"))  {  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  xmlDoc.async=false;  xmlDoc.load("articles/article1.xml");  }else {	alert("It doesn't work");	}var x=xmlDoc.getElementsByTagName("article");--></script></head><body><div id="menu">	<ul>		<li>Accueil</li>		<li>Concepts de bases</li>		<li>XHTML</li>		<li>CSS</li>		<li>JavaScript</li>	</ul></div><div id="header"><img src="images/header2.jpg" height="250" width="1024" alt="En tĂȘte - Internet Facile"></div><div id="contenant"><div id="info"><script type="text/javascript" language="javascript"><!--for (i=0;i<x.length;i++)  {  document.write(x[i].getElementsByTagName("icon")[0].childNodes[0].nodeValue);  document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);  document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);  }--></script>	<!-- C'est ici que les listes d'articles en XML vont s'afficher--></div><div id="contenu"><script type="text/javascript"><!--  for (i=0;i<x.length;i++){document.write(x[i].getElementsByTagName("text")[0].childNodes[0].nodeValue);  }--></script></div></div></body></html>

and here's the XML just in case

<?xml version="1.0" encoding="utf-8"?><article>	<icon><img src="images/icones/icone1.jpg" width="40px" height="40px" alt="Icone1" /></icon>	<title><b>Article 1, very long title to test the display</b></title>	<description><p class="a_description">Article 1, test d'affichage d'article en XML.</p></description>	<text>	<h1>Lorem Ipsum</h1>	Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eu diam quam. Etiam lacus arcu, gravida eget cursus ac, aliquet vulputate dui. Nunc at neque enim, at condimentum nisi. Duis sit amet lorem sed sem mattis volutpat. Praesent pharetra dolor ac odio mattis placerat in mollis odio. Aenean lectus justo, sodales vel tristique at, auctor sit amet lacus. Mauris nisl odio, imperdiet in vehicula non, mollis sit amet nibh. Praesent nisl mauris, consequat nec faucibus eu, mattis quis turpis. Etiam posuere auctor massa, elementum imperdiet magna ullamcorper a. Aliquam accumsan dignissim ipsum, et tincidunt libero sagittis ut. Morbi quis dapibus massa. Etiam sed suscipit neque. Sed volutpat viverra odio, a laoreet neque cursus eget.	<p>	In mi elit, consectetur fermentum rhoncus vel, mattis non lorem. Vestibulum a lacus nec felis sollicitudin pulvinar. Sed vehicula nunc nec lorem mollis quis imperdiet ligula lobortis. Cras non nunc non nisl aliquet iaculis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc adipiscing, tellus sed tristique porta, velit nibh vestibulum odio, eu pulvinar urna nunc a orci. Fusce nec dui arcu, vitae aliquet neque. Duis sit amet urna leo, at vulputate tellus. Integer volutpat arcu vel elit laoreet vel luctus dolor commodo. Sed leo magna, sollicitudin eget fringilla et, rutrum ut tortor. Morbi sed nisi nunc, eget tempus velit. Sed ac volutpat purus. Integer vitae ultrices urna.	</p>	<p>	Suspendisse et nunc ut neque tincidunt tempor et eget mi. Cras iaculis commodo sapien condimentum vestibulum. Nam vel est tortor, non sollicitudin risus. Sed non consectetur tellus. Vivamus sit amet lacus eros. Maecenas ut quam sit amet arcu viverra tempus. Sed ut nulla sed ante facilisis convallis. Nam blandit commodo dui ac fringilla. Nullam vehicula suscipit elit, vitae adipiscing purus blandit in. Aliquam ut tellus at elit facilisis suscipit eget at nunc. Aliquam consequat ultrices sodales.	</p>	<p>	Phasellus dignissim pharetra sodales. ###### sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris eget augue magna. Nulla pretium libero non massa porta et pretium sapien tristique. Integer consectetur scelerisque mauris, eget euismod ipsum tincidunt id. Aenean orci dolor, tincidunt quis cursus et, aliquet at ante. Nunc fringilla orci et justo scelerisque rhoncus. In quis nunc in mauris semper eleifend. Etiam sit amet odio massa. Nam semper, purus vel scelerisque pretium, nisl orci pretium tortor, vel fermentum nisl quam ac lorem. Pellentesque placerat auctor rhoncus. Praesent ac nunc ac lorem porttitor cursus id tincidunt elit. Nam sapien dui, interdum vulputate vehicula sit amet, sollicitudin eu nunc. Vestibulum nec venenatis mi.	</p>	<p>	Pellentesque auctor, ipsum et volutpat accumsan, mauris magna vehicula mauris, a posuere elit lorem id risus. Fusce gravida justo eget sapien fringilla auctor. Proin eleifend feugiat pharetra. Mauris sagittis laoreet sollicitudin. Nullam et nulla ut felis facilisis consectetur. Mauris ornare volutpat nibh, vitae varius elit ultrices et. Praesent placerat nisi justo. Nulla facilisi. Nunc blandit tortor eget ipsum convallis sed molestie ante molestie. Nullam adipiscing vehicula turpis, vel sollicitudin purus dictum et. Etiam porta nunc eu metus imperdiet mollis. Pellentesque consequat nisl nec ligula aliquam molestie dignissim purus luctus. 	</p>	</text></article>

Thanks for any help you can give.ps : using IE8 and FF3

Link to comment
Share on other sites

I was testing in Internet Explorer. Apparently window.XMLHttpRequest exists in Internet Explorer but Internet Explorer doesn't have permission to use it.Substitute your if() statements for try/catch

  try {	xmlDoc=new XMLHttpRequest();	xmlDoc.open("GET","articles/article1.xml",false);	xmlDoc.send("");	xmlDoc=xmlDoc.responseXML;  } catch (e) {	try {	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");	  xmlDoc.async=false;	  xmlDoc.load("articles/article1.xml");	} catch(e) {	  alert("It doesn't work");		}  }  var x=xmlDoc.getElementsByTagName("article");

By the way, the language attribute of the <script> element is deprecated. Remove it from your <script> tags.

Edited by Ingolme
Link to comment
Share on other sites

I was testing in Internet Explorer. Apparently window.XMLHttpRequest exists in Internet Explorer but Internet Explorer doesn't have permission to use it.Substitute your if() statements for try/catch
  try {	xmlDoc=new XMLHttpRequest();	xmlDoc.open("GET","articles/article1.xml",false);	xmlDoc.send("");	xmlDoc=xmlDoc.responseXML;  } catch (e) {	try {	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");	  xmlDoc.async=false;	  xmlDoc.load("articles/article1.xml");	} catch(e) {	  alert("It doesn't work");		}  }  var x=xmlDoc.getElementsByTagName("article");

By the way, the language attribute of the <script> element is deprecated. Remove it from your <script> tags.

Thank you very much. Working perfectly! Never used Try and Catch before. Also thank you for the tip on the attribute.
Link to comment
Share on other sites

replace (ActiveXObject("Microsoft.XMLDOM")) with (window.ActiveXObject)and problem solved, i'm on a roll today!
If you tried his initial code you might have noticed that Internet Explorer was returning true when testing window.XMLHttpRequest. This means that it would still not work if you had only applied the change you just suggested.
Link to comment
Share on other sites

and yet! when i DID try it out in all browsers, IT WORKED! wait minute test it again, hold on, yes, yes, yes, yes, yes, yes, yes, bummer! yep it still works.must admit i have not tested it IE8, (refuse to download, give MS time to get all the bugs out, which might be why its not working, typical MS FU)

Edited by dsonesuk
Link to comment
Share on other sites

must admit i have not tested it IE8, (refuse to download
IE8, even in its current state, is far superior to any other version of IE. It will also emulate IE7. IE7 in its current state is not a better browser than IE8 in its current state.
Link to comment
Share on other sites

Javascript is the only browser scripting language recommended by the W3C, XML is definitely a W3C recommendation, but not necessarily needed in web pages. XML is mainly used for other technologies.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...