Safari 3.0.4 (Mac/Win) Can't Create a DOM doc. Why? Remedy?

Discussion in 'Web Design and Development' started by UncleRic, Jan 10, 2008.

  1. UncleRic macrumors newbie

    Joined:
    Jan 10, 2008
    Location:
    Las Vegas, doing a contract in Santa Clara, CA
    #1
    Greetings:

    Safari fires off an exception when attempting to create a DOM:


    // code for IE
    if (window.ActiveXObject) {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument) {
    xmlDoc=document.implementation.createDocument("","",null);
    } else {
    alert('Your browser cannot handle this script');
    }


    The exception raised on:

    document.implementation.createDocument()

    Thusly:
    TypeError: Value undefined (result of expression xmlDoc.load) is not object.


    So even though the script thinks the browser is W3C compliant (passing the
    'document.implementation.createDocument()') criteria, it fails.

    1) Doesn't anyone know a solution/remedy for this?
    2) How do I create a DOM doc in Safari?

    -- Ric.
     
  2. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    Try
    Code:
    xmlDoc=document.implementation.createDocument("","SomeRootTagName",null);
    or
    Code:
    xmlDoc=document.implementation.createDocument("",null,null);
     
  3. UncleRic thread starter macrumors newbie

    Joined:
    Jan 10, 2008
    Location:
    Las Vegas, doing a contract in Santa Clara, CA
    #3
    Safari lacks Mozilla's support for W3C DOM creation

    I've read documentation of document.implementation.createDocument() that Mozilla browsers follow; but Safari does NOT. I've tried various syntaxes including what you had suggested, but they don't work.

    I can get Safari to parse a XML doc. But I CAN'T get it to CREATE ONE. Nobody seems to know a solution to this; hence I assume Safari isn't ready for prime-time XML processing.

    That is, using JavaScript within Safari to create/load a XML document.
    BTW: Having E4X support is apparently, a secondary priority for Safari.

    Bummer.

    Ric.
     
  4. msteudel macrumors newbie

    Joined:
    Feb 2, 2008
    #4
    Hey we have a problem reading a xml with javascript in safari would you mind looking at this code and see what we are doing wrong? Thanks.

    Code:
    try{//try IE first
    		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    	
    	}
    	catch(e){
    		try{//try Mozilla, Firefox, Opera, etc.
    			xmlDoc=document.implementation.createDocument("",null,null);
    	
    		}
    	  	catch(e){
    			alert(e.message);
    			return;
    		}
    	}
    	
    	xmlDoc.async=false;
    	  xmlDoc.load("http://url/testFeed.xml");
    
    We're getting this error:

    Code:
    Value undefined (result of expression xmlDoc.load) is not object
    TIA, Mark
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Is xmlDoc define outside the try block? If not, that's a problem because it's out of scope when you are trying your load.
     
  6. msteudel macrumors newbie

    Joined:
    Feb 2, 2008
    #6
    Thanks. We took Rics other suggestions and looked up apple's documentation for doing this and utilized their code, but yeah, I guess it must've been failing into that third block where we didn't do anything ...

    Here's a link to Apple's site on this if this comes up in anyone else's search:

    http://developer.apple.com/internet/webcontent/xmlhttpreq.html
     
  7. chunpan macrumors newbie

    Joined:
    Jan 28, 2009
    #7
    Simple Solution to get an XML Doc Object across Browsers

    1. Include prototype.js in your page.
    2. Assuming you have an XML file to be parsed at this relative path, "mypath/myfile.xml", it can have real contents to be parsed or an empty XML document with proper content-type header (thus you will be able to create an empty DOM object later).
    3. Use the following javascript:
      Code:
          var xmlDoc;
          var xmlURL = "mypath/myfile.xml";
          new Ajax.Request(xmlURL, {
              method: "get",
              asynchronous: false,
              onSuccess: function(resp, jsonObj) {
                  xmlDoc = resp.responseXML;
              }
          });
      
     

Share This Page