Connecting Tech Pros Worldwide Help | Site Map

Unable to read XML on web but can locally

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 11:24 AM
Andrew Poulos
Guest
 
Posts: n/a
Default Unable to read XML on web but can locally

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, 11:24 AM
Martin Honnen
Guest
 
Posts: n/a
Default 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, 11:27 AM
Andrew Poulos
Guest
 
Posts: n/a
Default 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, 11:27 AM
Andrew Poulos
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.