www.webdeveloper.com
Results 1 to 3 of 3

Thread: XML Problem in FireFox

  1. #1
    Join Date
    Dec 2006
    Posts
    56

    XML Problem in FireFox

    Hi all,

    I have a very basic xml file that has some customer names in it, like this:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <!DOCTYPE customers [
      <!ELEMENT customers (customer+)>
      <!ELEMENT customer (name)>
      <!ELEMENT name (#PCDATA)>
    ]>
    <customers>
      <customer>
        <name>Jon Doe</name>
      </customer>
      <customer>
        <name>Jane Doe</name>
      </customer>
    </customers>
    And I have a basic script file that opens the xml doc and gets the customer names:

    Code:
    function getxmldoc() {
      if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = false;
        doc.load("customers.xml");
        getnames();
      } else {
        doc = document.implementation.createDocument("","",null);
        doc.load("customers.xml");
        doc.onload = getnames;
      }
    }
    function getnames() {
      var names = doc.getElementsByTagName("name");
      alert(names.length);
      document.close();
    }
    The code works beautifully in IE7 and alerts '2' but Firefox (v2.0) alerts '0'

    Why is this happening?

  2. #2
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Alerts "2" in Firefox2.0 and IE7.0 for me. Make sure you aren't using a cached version of the page. Give this a shot to see if the page is cached.
    Code:
    <script type="text/javascript">
    function getxmldoc() {
      if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.onreadystatechange=function(){if(doc.readyState==4)getnames()};
        doc.load("customers.xml");
    
      } else if(document.implementation && document.implementation.createDocument){
        doc = document.implementation.createDocument("","",null);
        doc.load("customers.xml");
        doc.onload = getnames;
      } else if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","customers.xml");
    xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState!=4)return;
    if(xmlhttp.status!=200 && xmlhttp.status!=0)return;
    doc=xmlhttp.responseXML;
    getnames();
    }
    xmlhttp.send(null);
    }
    
    }
    
    /* IE support */
    if(!window.XMLSerializer){
    XMLSerializer=new Function();
    XMLSerializer.prototype.serializeToString=function(e){
    return e.xml;
    }
    }
    
    function getnames() {
    var j=new XMLSerializer();
    alert(j.serializeToString(doc))
    
    }
    getxmldoc()
    </script>

  3. #3
    Join Date
    Dec 2006
    Posts
    56
    Thanks for the info, I've also been told that FF2 requires the async=false to be set but haven't tested this yet

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles