C
CES
After three days I’m totally lost… I’m trying open an XML file in IE Mozilla and Safari. I was able to get one version to work in IE and Mozilla (MAC and Windows), the code that is commented out, but not Safari or Opera I then found that the preferred way to open is by using XMLHttpRequest but I can’t get any instance to work.
I found the below code on Apples Dev Site and have seen variations on others sites however the line:
xDoc.onreadystatechange = processReqChange;
which I’ve seen in every XMLHttpRequest instance seems to cause the script to stop running but I'm sure most of the (window.XMLHttpRequest) is wrong.
As you can tell I don’t have a clue and would greatly appreciate any help or at least a pointer to a VERY SIMPLE version of code that uses XMLHttpRequest, that actualy works..., Thanks in advance. - CES
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript">
function loadXMLData(){
var xmlFileName = 'demo.xml';
var xDoc;
if(window.ActiveXObject && /Win/.test(navigator.userAgent)){
xDoc = new ActiveXObject("Microsoft.XMLDom");
xDoc.async = false;
xDoc.load(xmlFileName);
writeXMLData(xDoc);
return true;
}
/*
// This works in IE and Mozilla (Windows and MAC) but not Safari
else if(document.implementation && document.implementation.createDocument){
xDoc = document.implementation.createDocument("","", null);
xDoc.load(xmlFileName);
xDoc.onload = function(){
writeXMLData(xDoc);
}
return true;
}
*/
else if (window.XMLHttpRequest) {
xDoc = new XMLHttpRequest();
xDoc.onreadystatechange = processReqChange; // If this line is here it causes the code to stop working
xDoc.open("GET", xmlFileName, true);
xDoc.send(null);
if (xDoc.readyState == 4) {
if (xDoc.status == 200) {
writeXMLData(xDoc);
return true;
}
}
alert("Mozilla Fork");
}
else{
return false;
}
}
</script>
</head>
<body onLoad="">
<div id="xmlInfo">
<script type="text/javascript">
var e = document.getElementById("xmlInfo");
function writeXMLData(xDoc){
var movies = xDoc.getElementsByTagName("movie");
for(var i = 0;i < movies.length;++i){
e.appendChild(document.createElement("div")).innerHTML = movies.firstChild.nodeValue;
}
}
if(! loadXMLData()){
alert("NO XML");
}
</script>
</div>
</body>
</html>
I found the below code on Apples Dev Site and have seen variations on others sites however the line:
xDoc.onreadystatechange = processReqChange;
which I’ve seen in every XMLHttpRequest instance seems to cause the script to stop running but I'm sure most of the (window.XMLHttpRequest) is wrong.
As you can tell I don’t have a clue and would greatly appreciate any help or at least a pointer to a VERY SIMPLE version of code that uses XMLHttpRequest, that actualy works..., Thanks in advance. - CES
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript">
function loadXMLData(){
var xmlFileName = 'demo.xml';
var xDoc;
if(window.ActiveXObject && /Win/.test(navigator.userAgent)){
xDoc = new ActiveXObject("Microsoft.XMLDom");
xDoc.async = false;
xDoc.load(xmlFileName);
writeXMLData(xDoc);
return true;
}
/*
// This works in IE and Mozilla (Windows and MAC) but not Safari
else if(document.implementation && document.implementation.createDocument){
xDoc = document.implementation.createDocument("","", null);
xDoc.load(xmlFileName);
xDoc.onload = function(){
writeXMLData(xDoc);
}
return true;
}
*/
else if (window.XMLHttpRequest) {
xDoc = new XMLHttpRequest();
xDoc.onreadystatechange = processReqChange; // If this line is here it causes the code to stop working
xDoc.open("GET", xmlFileName, true);
xDoc.send(null);
if (xDoc.readyState == 4) {
if (xDoc.status == 200) {
writeXMLData(xDoc);
return true;
}
}
alert("Mozilla Fork");
}
else{
return false;
}
}
</script>
</head>
<body onLoad="">
<div id="xmlInfo">
<script type="text/javascript">
var e = document.getElementById("xmlInfo");
function writeXMLData(xDoc){
var movies = xDoc.getElementsByTagName("movie");
for(var i = 0;i < movies.length;++i){
e.appendChild(document.createElement("div")).innerHTML = movies.firstChild.nodeValue;
}
}
if(! loadXMLData()){
alert("NO XML");
}
</script>
</div>
</body>
</html>