XML access voor Firefox

Discussion in 'Javascript' started by littlefool, Nov 21, 2005.

  1. littlefool

    littlefool Guest

    Hi,

    I'm trying to access a XML file in Firefox. It works perfect in IE
    (with the ActiveXObject), but in Firefox displays it an empty page. I
    found on the web examples how to do that, but I still have problems
    with it. Can anyone help me with this?

    This is the XML-file
    <?xml version="1.0" encoding="UTF-8"?>
    <constituencyseats>
    <seat>
    <Region>Central Scotland</Region>
    <Constituency>Aidrie and Shotts</Constituency>
    <Party>Labour</Party>
    </seat>
    <seat>
    <Region>Central Scotland</Region>
    <Constituency>Coatbridge and Chryston</Constituency>
    <Party>Labour</Party>
    </seat>
    </constituencyseats>

    This is the the code

    this.xmlObj = loadXMLSeats('scripts/xml/Constituencyseats.xml',
    'seat');

    function loadXMLSeats(xmlFile, tagname){
    var xmlDoc;
    if(document.implementation && document.implementation.createDocument)
    {
    xmlDoc = document.implementation.createDocument("", "", null);
    }
    else if (window.ActiveXObject) {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.onreadystatechange=verify;
    }
    else {
    alert('your browser can\'t handle this script');
    return;
    }
    xmlDoc.load(xmlFile);
    return xmlDoc.getElementsByTagName(tagname);
    }
    function verify(){
    // 0 Object is not initialized
    // 1 Loading object is loading data
    // 2 Loaded object has loaded data
    // 3 data from object can be worked with
    // 4 Object completely initialized
    if (xmlDoc.readyState != 4) return false;
    }

    It appears that in Firefox the problem occurs with loading the XML
    file. After loading, the object stays empty.

    Thank you for your help
    littlefool, Nov 21, 2005
    #1
    1. Advertising

  2. littlefool wrote:


    > I'm trying to access a XML file in Firefox. It works perfect in IE
    > (with the ActiveXObject), but in Firefox displays it an empty page. I
    > found on the web examples how to do that, but I still have problems
    > with it.


    Look here for suggestions on how to to that:
    <http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>


    > var xmlDoc;
    > if(document.implementation && document.implementation.createDocument)
    > {
    > xmlDoc = document.implementation.createDocument("", "", null);
    > }


    There are more browsers than Mozilla that support the above but a load
    method is only exposed by some Mozilla versions thus that call

    > xmlDoc.load(xmlFile);


    will give an error in Opera for instance.

    As for the Mozilla problem loading by default happens asynchronously so
    you would need to set up an onload handler before calling the load
    method. Or in newer Mozilla versions you can do synchronous loading by
    setting
    xmlDoc.async = false
    before calling the load method. But synchronous loading blocks the
    browser so it might be fine for a first test to play with XML, in your
    final code on the web you should use asynchronous loading.

    But check the article I linked to above, it is not necessary and helpful
    to go the createDocument/load way for Mozilla as it does not work in
    other browser at all while XMLHttpRequest to load XML works in lots of
    browsers by now and gives you more possibilities and better error
    handling (e.g. access to HTTP headers).

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 21, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pim van der Eijk
    Replies:
    1
    Views:
    440
    Joris Gillis
    Oct 21, 2004
  2. Rensjuh
    Replies:
    7
    Views:
    953
    Mabden
    Sep 2, 2004
  3. susan
    Replies:
    3
    Views:
    709
    Harlan Messinger
    Jan 20, 2009
  4. Replies:
    4
    Views:
    95
  5. Replies:
    6
    Views:
    133
Loading...

Share This Page