trouble using mozilla xml loading code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    trouble using mozilla xml loading code

    Hi there,

    I am using the following function to import a xml file whether the
    users browser be IE or Mozilla:

    function importXML(file) {
    var xmlDoc;
    var moz = (typeof document.implem entation != 'undefined') && (typeof
    document.implem entation.create Document != 'undefined');
    var ie = (typeof window.ActiveXO bject != 'undefined');

    if (moz) {
    xmlDoc = document.implem entation.create Document("", "", null)
    //do I need something else here???
    } else if (ie) {
    xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.async = false;
    while(xmlDoc.re adyState != 4) {};
    }
    xmlDoc.load(fil e);
    return xmlDoc
    }



    An example call of this function looks like this:

    importXML('xml/books.xml')

    The code for IE works well as I have used code to access the XML file
    im using but the code for Mozilla doesnt work. I got the funxtion from
    a tutorial site, http://www.sitepoint.com/article/xml-javascript-mozilla.

    Am I missing something after the line 'xmlDoc =
    document.implem entation.cre... ?? On the tutorial it had a line
    xmlDoc.onload = readXML but that appears to be some kind of function,
    readXML, and isnt shown on the tutorial. How do I get the variable
    xmlDoc to the same stage as the code used for IE so that I can then
    perform something like after the function is run??:

    nodes = xmlDoc.getEleme ntsByTagName("t itle")

    or

    nodes = xmlDoc.document Element.childNo des


    If I try the above in Mozilla and do something like nodes.length I get
    0 (wrong) whereas if I do it in IE, I get 10 (correct). Is the 'nodes
    = ...' code only able to run in IE or is the variable, xmlDoc, not
    properly prepared in Mozilla to do the above?? The results I get in
    Mozilla lead me to assume either but I may be wrong. Any help would be
    great.

    Cheers

    Burnsy
  • Martin Honnen

    #2
    Re: trouble using mozilla xml loading code



    mr_burns wrote:

    [color=blue]
    > I am using the following function to import a xml file whether the
    > users browser be IE or Mozilla:
    >
    > function importXML(file) {
    > var xmlDoc;
    > var moz = (typeof document.implem entation != 'undefined') && (typeof
    > document.implem entation.create Document != 'undefined');[/color]

    Other browsers have document.implem entation.create Document as well so at
    least the name of the variable 'moz' is doubtful.

    While Mozilla allows you to create an XML document with the method you
    check for above and then exposes a load method that stuff is Mozilla
    only while by now Safari and recent Opera version implement
    XMLHttpRequest thus I would strongly suggest to use that if you want to
    load XML:

    function loadXML (url, processXML) {
    var httpRequest;
    if (typeof XMLHttpRequest != 'undefined') {
    httpRequest = new XMLHttpRequest( );
    }
    else if (typeof ActiveXObject != 'undefined') {
    httpRequest = new ActiveXObject(' Microsoft.XMLHT TP');
    }
    if (httpRequest) {
    httpRequest.ope n('GET', url, true);
    httpRequest.onr eadystatechange = function () {
    if (httpRequest.re adyState == 4 && httpRequest.res ponseXML) {
    processXML(http Request.respons eXML);
    }
    };
    httpRequest.sen d(null);
    }
    }

    function exampleXMLHandl er (xmlDocument) {
    if (xmlDocument.do cumentElement) {
    alert(xmlDocume nt.documentElem ent.childNodes. length);
    }
    }


    loadXML('test20 05010601.xml', exampleXMLHandl er)

    That should do with IE/Win (versions 5, 5.5, 6 and hopefully later),
    Mozilla 1.0 and later, Safari 1.2 (not tested now), Opera 7.6x , 8.00 or
    later, as long as the XML loaded is well-formed. If the parsing of the
    XML fails then unfortunately every implementation has its own way to
    indicate parse errors so catering for that is a bit too much to handle
    here in a newsgroup post.

    --

    Martin Honnen

    Comment

    Working...