I’m using the following code to read an XML file –
var xmlhttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
xmlDoc=xmlhttp.responseXML;
tmp = xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
alert ('-' + tmp + '-');
}
}
xmlhttp.open("GET", "test3.php", true);
xmlhttp.send();
-
XML File
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> </note>
If the XML file is called xxxx.PHP or anything other than xxxx.XML I get the error “unable to get value of the property childNodes”. If the XML file is called xxxx.XML it works fine. Files are exactly the same, the only difference is the extension.
This has been tested in FF, Chrome and IE with the same results. It’s not a cache issue.
I need the extension to be PHP so I can generate dynamic content for AJAX.
Could this be something to do with php.ini / apache.conf or is there something I’m missing ??
In order for the XML to be interpreted as XML by your browser, the server must send it with the right
Content-typeheader (i.e.text/xml). By default, your server will only do this for thexmlfile extension.In your PHP code, before you output any of the XML, you can set the
Content-typeheader like so: