Reading dynamic XML into JavaScript for Mozilla

Discussion in 'JavaScript' started by jwalborn, Feb 6, 2008.

  1. #1
    Okay, I'm having trouble making my application fly in Mozilla, using Firefox 2.0.0.11 as a test platform. Using web searches for "JavaScript XML" and the like I've found many good examples for loading and parsing XML in JavaScript... I'm already familiar with loading and parsing XML in ActionScript and classic ASP, so the basics haven't been that difficult.

    I've got the whole thing working just fine if I use a static XML page. If I call an ASP page to build a dynamic XML, however, only the MSIE portion of the code functions. The code is not changing, so it's something to do with the difference between static and dynamically constructed XML.

    With the understanding that it's dirty because I'm still trying to hack it all together, here's the XML code for your review:

    
    
            var xmlPath = "GUI_GET_AssetLocations.asp"
    
            //(loadXML function called on page load)
    
            function loadXML()
            {
                // Check for Mozilla browser type
                if (document.implementation && document.implementation.createDocument)
                {
                    // Build XMLDOM object and establish "onload" event for Mozilla
                    xmlDoc = document.implementation.createDocument("", "", null);
                    xmlDoc.ignorewhitespace = true;
                    xmlDoc.onload = display;
                }
                // Check for Internet Explorer browser type
                else
                {
                    // Build XMLDOM object and establish "onload" event for MSIE
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.onreadystatechange = function ()
                    {
                        if (xmlDoc.readyState == 4) display()
                    };    
                }
                // Load XML based on passed path
                xmlDoc.load(xmlPath);
            }
    
            function display()
            {
                //
                var xmlTags = xmlDoc.getElementsByTagName('location');
                //
                for (var i=0;i<xmlTags.length;i++)
                {
                    //
                    var nodeLoop = xmlTags[i].childNodes.length;
                    //
                    for (var ii=0; ii<nodeLoop; ii++)
                    {
                        //
                        var nodeContents = xmlTags[i].childNodes[ii];
                        //
                        var nodeLabel = nodeContents.nodeName;
                        //
                        if (nodeContents.childNodes.length > 0) 
                        {
                            var nodeData = nodeContents.firstChild.nodeValue;
                        } else {
                            var nodeData = ""
                        }
                        //
                        if (nodeLabel == "id")
                        {
                            var dataId = nodeData
                        }
                        //
                        if (nodeLabel == "name")
                        {
                            var dataName = nodeData
                        }  
                        //
                        if (nodeLabel == "lat")
                        {
                            var dataLat = nodeData
                        } 
                        //
                        if (nodeLabel == "long")
                        {
                            var dataLong = nodeData
                        }
                        //
                        if (nodeLabel == "almstattxt")
                        {
                            var dataAlarm = nodeData
                        } 
                        //
                        if (nodeLabel == "icon")
                        {
                            var dataIcon = customIconPre + nodeData + customIconSuf
                        }     
                    }
                    //                                            
                    AddPin(dataId, dataName, dataLat, dataLong, dataAlarm, dataIcon);
                    //
                    //alert(dataId + ";" + dataName + ";" + dataLat + ";" + dataLong + ";" + dataAlarm);
                }
            }
    
    
    Code (markup):
     
    jwalborn, Feb 6, 2008 IP
  2. jwalborn

    jwalborn Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's an exact copy (other than confidental information being removed) of the ASP return, keeping in mind that this *exact* XML return works perfectly if saved as a static XML file and accessed in that way...

    
    <?xml version="1.0" encoding="UTF-8" ?> 
       <content>
          <location>
            <id>3</id> 
            <name>Denver CO</name> 
            <desc>Denver CO</desc> 
            <desctype>Text</desctype> 
            <icon>iconLoc2</icon> 
            <lat>39.741</lat> 
            <latid>5</latid> 
            <long>-104.99</long> 
            <longid>6</longid> 
            <dlat /> 
            <dlatid /> 
            <dlong /> 
            <dlongid /> 
            <almstattxt>No Alarm</almstattxt> 
            <almstatval>-1</almstatval> 
            <fencerad /> 
            <fenceradid /> 
            <doorstattxt /> 
            <doorstatval /> 
            <doorstatid /> 
            <dist /> 
            <icox>0</icox> 
            <icoy>0</icoy> 
          </location>
          <location>
            <id>325</id> 
            <name>GPS Demo</name> 
            <desc>GPS</desc> 
            <desctype>Text</desctype> 
            <icon>iconTug2</icon> 
            <lat>27.803639</lat> 
            <latid>7</latid> 
            <long>-90.933022</long> 
            <longid>8</longid> 
            <dlat /> 
            <dlatid /> 
            <dlong /> 
            <dlongid /> 
            <almstattxt>No Alarm</almstattxt> 
            <almstatval>-1</almstatval> 
            <fencerad /> 
            <fenceradid /> 
            <doorstattxt /> 
            <doorstatval /> 
            <doorstatid /> 
            <dist /> 
            <icox>-27</icox> 
            <icoy>-59</icoy> 
          </location>
          <location>
            <id>326</id> 
            <name>IMS Tracker</name> 
            <desc>IMS</desc> 
            <desctype>Text</desctype> 
            <icon>iconTug2</icon> 
            <lat>29.644547</lat> 
            <latid>9</latid> 
            <long>-90.815649</long> 
            <longid>10</longid> 
            <dlat /> 
            <dlatid /> 
            <dlong /> 
            <dlongid /> 
            <almstattxt>No Alarm</almstattxt> 
            <almstatval>-1</almstatval> 
            <fencerad /> 
            <fenceradid /> 
            <doorstattxt /> 
            <doorstatval /> 
            <doorstatid /> 
            <dist /> 
            <icox>-27</icox> 
            <icoy>-59</icoy> 
         </location>
       </content>
    
    Code (markup):
     
    jwalborn, Feb 6, 2008 IP
  3. jwalborn

    jwalborn Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay, well, thanks to great help on other forums, the issue is resolved. Even better, I've got the solution working in Safari and Opera now also.

    For anyone trying to solve this problem in the future, here's the ticket...

    First, for cross-browser compatability, use the free script found here:

    http://www.howtocreate.co.uk/jslibs/script-importxml

    Second, when creating your dynamic XML, be sure the encoding type is "text/xml"... MSIE will assume this based on the XML header, but other browsers aren't quite so slick and need it spelled out for them. If you're creating your dynamic XML in ASP, you do this by telling the Response object to present the data in this format using the following line of code (before you write the XML its self to the Response object):

    
    Response.ContentType="text/xml"
    
    Code (markup):
     
    jwalborn, Feb 7, 2008 IP