XML file access not working in Firefox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laramie.hartmann@gmail.com

    XML file access not working in Firefox

    I have a script (see below) that accesses a XML file and displays the
    contents through a series of document.write calls. This all works fine
    in IE, but not at all in Firefox. I get no errors in the javascript
    console, but when I try to load the elements by tag name and look at
    the length it is always 0 as if no elements have been added. Any
    suggestions are greatly appreciated as I've been stuck on this for a
    while now.

    Thanks,

    Laramie Hartmann

    Here is a sample record from my XML file...

    <?xml version="1.0" ?>

    <DOCTOR>
    <PHOTO>facholon u.jpg</PHOTO>
    <NAME>Felix N. Acholonu, M.D.</NAME>
    <SNAME>FACH</SNAME>
    <OFFICE>
    <ADDRESS>
    <STREET>1801 Barrs St., Ste. 920</STREET>
    <CITY>Jacksonvi lle</CITY>
    <STATE>FL</STATE>
    <ZIP>32204</ZIP>
    <PHONE>(904) 387-1401</PHONE>
    </ADDRESS>
    <ABBR>STV5</ABBR>
    <FULL>St. Vincent's 5</FULL>
    </OFFICE>
    <SPECIALTY>Obst etrics &amp; Gynecology</SPECIALTY>
    <HOSPITAL>St. vincent's Medical Center</HOSPITAL>
    <COLLEGE>SUNY Health Science Center</COLLEGE>
    <GRADUATION>198 1</GRADUATION>
    <BOARD>Americ an Board of Obstetrics &amp; Gynecology</BOARD>
    <BLURB></BLURB>
    </DOCTOR>

    Here is the javascript...

    <script type="text/javascript">
    //<![CDATA[
    if (window.ActiveX Object)
    {
    var xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.async=fa lse; //Enforce download of XML file first. IE only.
    document.write( "Internet Explorer");
    }
    else if (document.imple mentation &&
    document.implem entation.create Document)
    {

    xmlDoc = document.implem entation.create Document("", "doc", null);

    document.write( "Firefox");
    }

    xmlDoc.load("ca regivers.xml");

    var x=xmlDoc.getEle mentsByTagName( "PHOTO")
    var nm=xmlDoc.getEl ementsByTagName ("NAME")
    var sname=xmlDoc.ge tElementsByTagN ame("SNAME")
    var abbr=xmlDoc.get ElementsByTagNa me("ABBR")
    var full=xmlDoc.get ElementsByTagNa me("FULL")
    var street=xmlDoc.g etElementsByTag Name("STREET")
    var city=xmlDoc.get ElementsByTagNa me("CITY")
    var state=xmlDoc.ge tElementsByTagN ame("STATE")
    var zip=xmlDoc.getE lementsByTagNam e("ZIP")
    var phone=xmlDoc.ge tElementsByTagN ame("PHONE")
    var specialty=xmlDo c.getElementsBy TagName("SPECIA LTY")


    var tem = window.location .search;
    tem = tem.substring(1 0,14);



    for (var i = 1; i <= x.length; i++)
    {

    if (tem == abbr.item(i-1).text || tem == "" || tem == "ALLD")
    {
    document.write( "<div class='square'> ")
    document.write( "<img class='bimg' src='" + x.item(i-1).text + "'
    width='125' height='150'></img>")
    document.write( "<div class='namebloc k'>")

    document.write( "<strong><a href='bio.html? caregiver=" +
    sname.item(i-1).text + "'>" + nm.item(i-1).text + "</a></strong><br
    />")
    document.write( "<a href='division. html?division=" +
    abbr.item(i-1).text + "'>" + full.item(i-1).text + "</a><br />")
    document.write( street.item(i-1).text + "<br />")
    document.write( city.item(i-1).text + ", " + state.item(i-1).text + " "
    + zip.item(i-1).text)

    document.write( "</div>")
    document.write( "<div class='clearer' ></div>")
    document.write( "</div>")
    }
    }


    var division = document.getEle mentById("divis ion")

    for (i = 0; i < division.length ; i++)
    {
    if (tem == division.option s[i].value)
    {
    division.option s[i].selected = true
    }
    }

    var name=xmlDoc.get ElementsByTagNa me("NAME")
    var docs = document.getEle mentById("careg iver")
    var len = docs.length

    for (var i = 0; i < sname.length; i++)
    {
    docs.options[len+i] = new Option(name.ite m(i).text)
    docs.options[len+i].value = sname.item(i).t ext
    }

    //]]>
    </script>

  • Martin Honnen

    #2
    Re: XML file access not working in Firefox



    laramie.hartman n@gmail.com wrote:
    I have a script (see below) that accesses a XML file and displays the
    contents through a series of document.write calls. This all works fine
    in IE, but not at all in Firefox. I get no errors in the javascript
    console, but when I try to load the elements by tag name and look at
    the length it is always 0 as if no elements have been added.
    <script type="text/javascript">
    //<![CDATA[
    if (window.ActiveX Object)
    {
    var xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.async=fa lse; //Enforce download of XML file first. IE only.
    document.write( "Internet Explorer");
    }
    else if (document.imple mentation &&
    document.implem entation.create Document)
    {
    >
    xmlDoc = document.implem entation.create Document("", "doc", null);
    >
    document.write( "Firefox");
    }
    >
    xmlDoc.load("ca regivers.xml");
    By default Mozilla loads the XML asynchronously so you need to add an
    onload handler and process the XML there (e.g. call getElementsByTa gName
    in the onload handler). Or you need to use xmlDoc.async = false before
    you call the load method.
    In my view if you want to load XML cross browser then using
    XMLHttpRequest/XMLHTTP is easier as it requires less code differences, see
    <http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>

    Opera 8 for instance supports XMLHttpRequest but no load method for XML
    DOM documents.

    So consider loading the XML asynchronously with XMLHttpRequest/XMLHTTP
    and then reading out values from the XML with the DOM and adding
    contents to the HTML document with the DOM.


    --

    Martin Honnen

    Comment

    • laramie.hartmann@gmail.com

      #3
      Re: XML file access not working in Firefox

      Martin - Thanks for the response. I'm being led in the right direction,
      but I still haven't been able to make everything work properly.

      In Mozilla does x.item(i-1).text work given that:

      var x=xmlDoc.getEle mentsByTagName( "PHOTO")

      I can't get it working correctly, but it does display fine in IE. To
      work around this problem I tested

      document.write( x[0].firstChild.nod eValue);

      instead. In Mozilla it writes the value, but the rest of the page never
      loads. No errors in the Javascript console though.

      Here is my new code for testing purposes. I must admit, the new test
      code below does not work in IE. It simply loads the HTML (unlike
      Mozilla), but does not throw any errors.

      <script type="text/javascript">
      //<![CDATA[

      if (window.ActiveX Object)
      {
      var xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
      xmlDoc.async=fa lse; //Enforce download of XML file first. IE only.
      xmlDoc.onload = readXML;
      document.write( "Internet Explorer");
      }
      else if (document.imple mentation &&
      document.implem entation.create Document)
      {
      xmlDoc = document.implem entation.create Document("", "doc", null);
      xmlDoc.onload = readXML;
      document.write( "Firefox.") ;
      }

      function readXML()
      {
      x=xmlDoc.getEle mentsByTagName( "PHOTO")
      nm=xmlDoc.getEl ementsByTagName ("NAME")
      sname=xmlDoc.ge tElementsByTagN ame("SNAME")
      abbr=xmlDoc.get ElementsByTagNa me("ABBR")
      full=xmlDoc.get ElementsByTagNa me("FULL")
      street=xmlDoc.g etElementsByTag Name("STREET")
      city=xmlDoc.get ElementsByTagNa me("CITY")
      state=xmlDoc.ge tElementsByTagN ame("STATE")
      zip=xmlDoc.getE lementsByTagNam e("ZIP")
      phone=xmlDoc.ge tElementsByTagN ame("PHONE")
      specialty=xmlDo c.getElementsBy TagName("SPECIA LTY")

      //Firefox displays this info (IE doesn't) , but writes to a blank page
      and the hour glass pointer
      // never goes away.

      document.write( "Variable x has " + x.length + " values. and the 5th
      value is ")
      document.write( x[0].firstChild.nod eValue);
      }

      xmlDoc.load("ca regivers.xml")


      //]]>
      </script>

      Comment

      • Martin Honnen

        #4
        Re: XML file access not working in Firefox



        laramie.hartman n@gmail.com wrote:

        In Mozilla does x.item(i-1).text work given that:
        Mozilla's DOM implementation (mainly) follows the W3C Level 2 DOM
        specification. There is no property named |text| in that object model,
        |text| is an extension MSXML introduced (and I think Opera 9 has now too).
        With Firefox however there is support for the W3C DOM Level 3 property
        named |textContent| so if you have a node and want the concatenation of
        all text in the node and child/descendant nodes then you can use e.g.
        var text;
        if (typeof node.textConten t != 'undefined') {
        text = node.textConten t;
        }
        else if (typeof node.text != 'undefined') {
        text = node.text;
        }
        else if (typeof node.innerText != 'undefined') {
        text = node.innerText;
        }

        As for the other problems I already suggested to load asynchronously and
        then once the XML is loaded not to use document.write to show the XML
        contents but to use DOM scripting with createElement and appendChild.
        That means you write a complete HTML document with script loading the
        XML and script creating HTML elements and inserting them once the XML
        has been loaded.



        --

        Martin Honnen

        Comment

        • laramie.hartmann@gmail.com

          #5
          Re: XML file access not working in Firefox

          Thank you for your replies. I can't get my onLoad function to work. I
          have stripped out any XML parsing and I am only trying to load the XML
          document and call my onLoad function. In Firefox it prints my one
          document.write line, but displays nothing else except a white
          background. As before the hour glass pointer never goes away. Here is
          my code.

          script type="text/javascript">
          //<![CDATA[

          var loader

          function loaded()
          {
          loader=5
          document.write( loader)
          }

          if (document.imple mentation && document.implem entation.create Document)
          {
          xmlDoc = document.implem entation.create Document("", "", null)
          xmlDoc.load("ca regivers.xml")
          document.write( " Firefox. ")
          xmlDoc.onload = loaded
          }


          //]]>
          </script>

          Comment

          • laramie.hartmann@gmail.com

            #6
            Re: XML file access not working in Firefox

            Apparently the document is written over with the document.write
            command. So I placed a DO loop to make sure that the XML is completely
            loaded before the document.write command works. So at this point I
            think I've got it figured out for the most part.

            Comment

            • laramie.hartmann@gmail.com

              #7
              Re: XML file access not working in Firefox

              So I thought I had it figured out, but Firefox is not pulling the
              getElementsByTa gName. I am at a loss. In the Javascript console I get
              "x[5] has no properties" even though I clearly set x to hold all tags
              with the name "PHOTO". I am not a programmer and this has been driving
              me mad for 2 days. I am not familiar with programming with the DOM
              which is why I am using document.write. Everything worked great in IE,
              but I just can't get Firefox to do the trick.

              <script type="text/javascript">
              //<![CDATA[

              var xmlDoc = document.implem entation.create Document("", "", null);
              document.write( "Firefox.")
              isLoaded = xmlDoc.load("ca regivers.xml")
              while (isLoaded != true){}
              readXML()

              function readXML()
              {
              x=xmlDoc.getEle mentsByTagName( "PHOTO")

              document.write( "Variable x has " + x.length + " values. and the 5th
              value is ")
              document.write( x[5].firstChild.nod eValue);
              }

              //]]>
              </script>

              Comment

              Working...