Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

load XML Document

Status
Not open for further replies.

LuckyKoen

Programmer
Feb 14, 2005
57
BE
I have a small issue with reading XML documents. My code works fine in IE - where it shows an alert box with my XML file in it - but not in Firefox. What could be the problem ?

Code:
function loadXML(file) {
   var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
   var ie =  (typeof window.ActiveXObject != 'undefined');
   var xmlDoc;

   if (moz) {
      xmlDoc = document.implementation.createDocument("", "", null);
      xmlDoc.async = false;
   }
   else if (ie) {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = false;
   }
   xmlDoc.load(file);
   return xmlDoc;
}
	
readDocument();

function readDocument() {
   var xmlDoc = loadXML("Calendar4.xml");
   var years = xmlDoc.getElementsByTagName('year');
   alert(xmlDoc.xml);
}

The XML Document :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Calendar>
  <Year id="2005">
    <Month id="1" name="January">
      <Week id="1">
        <Day name="Thursday"/>
      </Week>
    </Month>
  </Year>
</Calendar>
 
I'm not totally sure that this is right, but at a guess, shouldn't you be harnessing some sort of onload event for the xml document, rather than assuming that it has loaded?

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
>where it shows an alert box with my XML file in it - but not in Firefox. What could be the problem ?

The problem is xml property of objectXMLDocument object is proprietary to ie. Everything is already contained in the xmlDoc all right. For ff/nn or mozilla, if you like, you have to pass through the objectXMLSerializer object.
[tt]
//alert(xmlDoc.xml); //replaced
var sxml=(window.ActiveXObject)?xmlDoc.xml:new XMLSerializer().serializeToString(xmlDoc);
alert(sxml);
[/tt]
 
The problem is not only the alert(xmlDoc.xml). I tried alert(years.length) which gave 1 in IE but 0 in Firefox. So somehow I think the document doesn't load in Firefox.
 
> So somehow I think the document doesn't load in Firefox.
I don't think so. That's all I say.
 
>alert(years.length) which gave 1 in IE but 0 in Firefox
Do you see your own problem and jumping to conclusion?

[tt]>var years = xmlDoc.getElementsByTagName('year');[/tt]
Try this.
[tt]var years = xmlDoc.getElementsByTagName('[red]Y[/red]ear');[/tt]
 
The Year with a small y was only a typing mistake when I posted my code on this forum.

xmlDoc.getElementsByTagName('Year') still won't work with Firefox
 
>The Year with a small y was only a typing mistake
What can I say...
It works for me. Maybe you are special or I am.
 
Whatever you do, I would prefer this for the best practice.
[tt]
//readDocument();
window.onload=readDocument;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top