Connecting Tech Pros Worldwide Help | Site Map

Unable to read XML on web but can locally

  #1  
Old July 20th, 2005, 12:24 PM
Andrew Poulos
Guest
 
Posts: 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

  #2  
Old July 20th, 2005, 12:24 PM
Martin Honnen
Guest
 
Posts: n/a

re: Unable to read XML on web but can locally




Andrew Poulos wrote:
[color=blue]
> 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?[/color]

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/

  #3  
Old July 20th, 2005, 12:27 PM
Andrew Poulos
Guest
 
Posts: n/a

re: Unable to read XML on web but can locally


Martin Honnen wrote:
[color=blue]
>
>
> Andrew Poulos wrote:
>[color=green]
>> 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?[/color]
>
>
> Loading is done asynchronously so you need an onload event handler (for
> Mozilla) and an onreadystate event handler for IE.
>[/color]
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

  #4  
Old July 20th, 2005, 12:27 PM
Andrew Poulos
Guest
 
Posts: n/a

re: Unable to read XML on web but can locally


Andrew Poulos wrote:[color=blue]
> Martin Honnen wrote:
>[color=green]
>>
>>
>> Andrew Poulos wrote:
>>[color=darkred]
>>> 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?[/color]
>>
>>
>>
>> Loading is done asynchronously so you need an onload event handler
>> (for Mozilla) and an onreadystate event handler for IE.
>>[/color]
> 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
>[/color]
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


Closed Thread