Unable to read XML on web but can locally

A

Andrew Poulos

If I do something like the following snippet in JS:

xFile = "test.xml";
xDoc = document.implementation.createDocument("", "theXdoc", null);
xDoc.load(xFile);

// there's a pause here then

var data = xDoc.getElementsByTagName("text1")[0];

It works locally in IE6 and MZ1 but doesn't work when I upload the files
MZ1 tells me that 'data' has no properties. What is it that I'm doing wrong?

Andrew Poulos
 
M

Martin Honnen

Andrew said:
If I do something like the following snippet in JS:

xFile = "test.xml";
xDoc = document.implementation.createDocument("", "theXdoc", null);
xDoc.load(xFile);

// there's a pause here then

var data = xDoc.getElementsByTagName("text1")[0];

It works locally in IE6 and MZ1 but doesn't work when I upload the files
MZ1 tells me that 'data' has no properties. What is it that I'm doing
wrong?

Loading is done asynchronously so you need an onload event handler (for
Mozilla) and an onreadystate event handler for IE.
 
A

Andrew Poulos

Martin said:
Andrew said:
If I do something like the following snippet in JS:

xFile = "test.xml";
xDoc = document.implementation.createDocument("", "theXdoc", null);
xDoc.load(xFile);

// there's a pause here then

var data = xDoc.getElementsByTagName("text1")[0];

It works locally in IE6 and MZ1 but doesn't work when I upload the
files MZ1 tells me that 'data' has no properties. What is it that I'm
doing wrong?


Loading is done asynchronously so you need an onload event handler (for
Mozilla) and an onreadystate event handler for IE.
I tried re-writing it to something like this for MZ:

<script type="text/javascript">

xmlDoc = "undefined";

function initXML(xmlFile) {
if (document.implementation&&document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("","doc",null);
if (typeof xmlDoc != "undefined") {
xmlDoc.load(xmlFile);
xmlDoc.onload = stuffToDo;
}
}
}

function stuffToDo() {
alert(xmlDoc.getElementsByTagName("messages")[0]);
var msgobj = xmlDoc.getElementsByTagName("messages")[0];
// more stuff here...
}

</script>

<BODY onload="initXML('test.xml');">

The alert fires, so I think the xml file is being loaded, but it
displays the value as 'undefined'. It works locally so I'm at a loss as
to why it doesn't work when I post the files. Any help appreciated.

Andrew Poulos
 
A

Andrew Poulos

Andrew said:
Martin said:
Andrew said:
If I do something like the following snippet in JS:

xFile = "test.xml";
xDoc = document.implementation.createDocument("", "theXdoc", null);
xDoc.load(xFile);

// there's a pause here then

var data = xDoc.getElementsByTagName("text1")[0];

It works locally in IE6 and MZ1 but doesn't work when I upload the
files MZ1 tells me that 'data' has no properties. What is it that I'm
doing wrong?



Loading is done asynchronously so you need an onload event handler
(for Mozilla) and an onreadystate event handler for IE.
I tried re-writing it to something like this for MZ:

<script type="text/javascript">

xmlDoc = "undefined";

function initXML(xmlFile) {
if (document.implementation&&document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("","doc",null);
if (typeof xmlDoc != "undefined") {
xmlDoc.load(xmlFile);
xmlDoc.onload = stuffToDo;
}
}
}

function stuffToDo() {
alert(xmlDoc.getElementsByTagName("messages")[0]);
var msgobj = xmlDoc.getElementsByTagName("messages")[0];
// more stuff here...
}

</script>

<BODY onload="initXML('test.xml');">

The alert fires, so I think the xml file is being loaded, but it
displays the value as 'undefined'. It works locally so I'm at a loss as
to why it doesn't work when I post the files. Any help appreciated.

Andrew Poulos
I have a bit more info:
The following returns "HTML collection" for msgobj:
var msgobj = xmlDoc.getElementsByTagName("messages");

but "undefined" for
var msgobj = xmlDoc.getElementsByTagName("messages")[0];

To repeat, it works locally in MZ but fails when I post the file to the
server.

Andrew Poulos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top