By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,945 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,945 IT Pros & Developers. It's quick & easy.

Unable to read XML on web but can locally

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Andrew Poulos wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
Martin Honnen wrote:


Andrew Poulos wrote:
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",nu ll);
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

Jul 20 '05 #3

P: n/a
Andrew Poulos wrote:
Martin Honnen wrote:


Andrew Poulos wrote:
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",nu ll);
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
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.