www.webdeveloper.com
Results 1 to 5 of 5

Thread: xmldoc.getElementsByTagName error

  1. #1
    Join Date
    Jul 2006
    Posts
    13

    xmldoc.getElementsByTagName error

    Hi all!

    I am having a problem with the following script:

    Code:
      <!--"getEvents_text"-->
      function getEvents_text(xmldoc, mnth, day){
        var days_event, eventNode, m;  
            m = xmldoc.getElementsByTagName(getMonthName(mnth -1))[0];
            days_event = m.childNodes[day].childNodes[1].text;    
            return days_event;  
      }//end of getEvents_text()
    "getMonthName(mnth -1)" retrieves the month name to be used with the getElementsByTagName(...). IE sites an error with the following line:

    Code:
    days_event = m.childNodes[day].childNodes[1].text;
    The error message says that an Object is required, which I assume means I am missing somthing or added to much. Now the script thus far does return the variable I need but I get this crazy error message.

    Any ideas?

  2. #2
    Join Date
    Jul 2006
    Posts
    13

    a little more information....

    Here is a little more information and code if this helps:

    Code:
      <!--"Read from XML File "-->
      function getEvents(mnth, day){ //function relies on the current month and day to read the xml file
        if(window.ActiveXObject){
            var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
            xmldoc.async = false;
            xmldoc.load("events.xml");
            
            return getEvents_text(xmldoc, mnth, day);
        }//End if
        else if(document.implementation && document.implementation.createDocument){
            var xmldoc = document.implementation.createDocument("", "", null);
            xmldoc.load("events.xml");
            return getEvents_text(xmldoc, mnth, day);
        }//end else if
        else { alert("Your browser cannot handle this script!"); }
      }//getEvents
      
      <!--"getEvents_text"-->
      function getEvents_text(xmldoc, mnth, day){
        var days_event, eventNode, m;  
            m = xmldoc.getElementsByTagName(getMonthName(mnth -1))[0];
            days_event = m.childNodes[day].childNodes[1].text;
        return days_event;  
      }//end of getEvents_text()
    The above code loops through an xml file pulling data elements to update a table. Like I said in the previous post, it works although a little slowly from the server side with IE; however, I get an error message concerning the following line:
    Code:
    days_event = m.childNodes[day].childNodes[1].text;
    I also tried this out in Firefox but there it didn't run or update the calendar.

    Any Ideas?

  3. #3
    Join Date
    Jul 2006
    Posts
    13
    Anyone have had any luck with this one?

  4. #4
    Join Date
    Jul 2006
    Posts
    13
    OK...I found some discussion on webforums else where concerning white space being dealt with differently on IE and Firefox; does anybody know anything about this? I could use some guidance.

  5. #5
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    First let me point out that you are serving IE with Sjax(Synchronous JavaScript and XML)
    and Firefox with Ajax (Asynchronous JavaScript and XML).
    Firefox is lacking a:
    Code:
    xmldoc.async = false;
    otherwise you'll need to deal with the onreadystate event handler.


    Nextly IE and Firefox count childNodes differently. Whitespace in between tag nodes are treated as actual nodes by Firefox (standard behaviour) while IE doesn't treat these whitespaces as nodes (non-standard behaviour).
    Try replacing:
    Code:
    days_event = m.childNodes[day].childNodes[1].text;
    with:
    Code:
    days_event = m.getElementsByTagName("day")[day-1].getElementsByTagName("event")[0].firstChild.data;
    Might also be interesting in IE's "preserveWhiteSpace":
    Code:
    xmldoc.preserveWhiteSpace = true
    I'd also advice xmldoc.documentElement.getElementsByTagName over xmldoc.getElementsByTagName however I don't recall the different off the top of my head.

    Also the following that you are using doesn't support Opera:
    Code:
    if(window.ActiveXObject){
            var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
            xmldoc.async = false;
            xmldoc.load("events.xml");
            
            return getEvents_text(xmldoc, mnth, day);
        }//End if
        else if(document.implementation && document.implementation.createDocument){
            var xmldoc = document.implementation.createDocument("", "", null);
            xmldoc.load("events.xml");
            return getEvents_text(xmldoc, mnth, day);
        }//end else if
        else { alert("Your browser cannot handle this script!"); }
    Firefox uses document.implementation.createDocument (DOM2) while Opera8 doesn't support it and instead supports document.implementation.createLSParser (DOM3).
    In Opera it is not as easy as xmldoc.async
    Code:
    alert(document.implementation.MODE_ASYNCHRONOUS)//2
    alert(document.implementation.MODE_SYNCHRONOUS)//1
    hence:
    Code:
    var ls=document.implementation.createLSParser(1, null);
    xmldoc=ls.parseURI("events.xml");
    return getEvents_text(xmldoc, mnth, day);
    You might also be interesting in something like this:
    http://mapbuilder.sourceforge.net/ma...arissa.js.html

    And might I also add a word of discouragement toward using Sjax because it halts ALL JavaScript from running and causes your user's browser to freeze during the time of the request -- which is very UNprofessional. Look into Ajax instead and loading the XML document right away without any user-triggered events then when your user calls a function you can reference the XML document and return in the same go without resorting to Sjax.

    If you end up using Ajax instead, you might be insteaded in this simple code I wrote:
    http://www.aplustv.com/public/xmldom.htm
    which my JS file makes Ajax XMLDOM as easy as:
    Code:
    var xmldom=new XMLDOMParser();
    xmldom.onload=function(xmldoc){
    alert(xmldoc.documentElement)
    }
    xmldom.load("events.xml")
    if(!xmldom.parserType){alert("Your browser cannot handle this script!");}
    However detecting if the user is using IE and has ActiveX disabled is something I didn't code into the script and I'll have by next version by adding in some try/catch blocks and possibly some conditional compilation detection for IE (conditional JavaScript comments) in order to detect the JScript version of IE and to see if it comes with the "Microsoft.XMLDOM" ActiveX object and is provided by the browser but simply disabled by user's browser settings. If the user can enable ActiveX, it would be great to detect so and to give the user an alert:
    Code:
    alert("Please enable ActiveX for this script to work!\nTools -> Internet Options -> Securty tab -> Custom Level button -> Run ActiveX controls and plug-ins -> Enable")
    Still... falling back to IFRAMES is always a bright idea and coding it into my script I might add as an option however with less error handling via something along the lines of:
    Code:
    if(!xmldom.parserType){
    ifxmldom=new IFRAME_XMLDOMParser();
    ifxmldom.onload=function(xmldoc){
    alert(xmldoc.documentElement);
    }
    ifxmldom.load("events.xml");
    if(ifxmldom.ERROR){alert("Fatal Error!\nYour browser fell back to JavaScript IFRAME XMLDOM parsing and an error was generated while trying to parse the XML document in an IFRAME!");}
    }
    I'll throw a link to the finished "product" when I get around to it -- probably within the next few days.
    Last edited by Ultimater; 08-16-2006 at 04:26 PM.

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