Log in

View Full Version : XML code in HTML not working in IE



Fighterfox
12-09-2010, 12:36 PM
I'm not sure where the XML forum is, but maybe this will do. It's actually a combination of HTML, XML, and JavaScript. But anyway...

I am new XML, and need a little help getting it to work in IE—my code below already works in Firefox and Opera (I haven’t checked Chrome or Safari yet). For the following simple XML file:


<?xml version="1.0" encoding="ISO-8859-1"?>
<location>
<california>
<cityname>Oakland</cityname>
<description>Located east of San Francisco on San Francisco Bay</description>
</california>
</location>

And for the following JavaScript parsing code:


<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","test.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("california");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("cityname")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>

How do I get my XML file to display in IE? Are there any basic inconsistencies or errors in my code that I have not noticed which is why IE fails to show what Firefox does? Thanks.

traq
12-09-2010, 02:38 PM
you should ask a mod to move this to the javascript forum.

I don't know much about dealing with xml in javascript, but I would suspect this is a shortcoming of the browser. IE doesn't handle xml well at all. Do you have access to a server-side language like php? It would be simpler to parse the xml into html before it is output via ajax.

ggalan
12-10-2010, 05:23 PM
have you tried jQuery?
http://papermashup.com/parse-xml-with-jquery/