Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Readin dynamic XML in JavaScript for Mozilla

Status
Not open for further replies.

jwalborn

Programmer
Feb 6, 2008
4
US
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:
Code:
        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);
            }
        }
 
What mime-type is your asp page returning?

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
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...

Code:
<?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>
 
jwalborn said:
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...

Hence the reason why I asked what mime-type your asp page is returning.

Chances are it's returning something other than [tt]text/xml[/tt] which is causing Mozilla to have a hissy-fit.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
Yeah, I've never had to worry about it before. Every other piece of software I've ever used (ActionScript, VB6, VB.NET, MSIE-base JavaScript) has always picked up ASP-built XML pages without any additional work needed.

But apparently, for Mozilla, I have to specifically tell the ASP Response object to use "text/xml" formatting using the following command:

Response.ContentType="text/xml"

Once I did that, Mozilla was a happy camper. Next, testing it on Safari... :((
 
Okay, well, thanks to great help from "dwarfthrower" (tongue-in-cheek reference to early PnP D&D noted), 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:


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):

Code:
Response.ContentType="text/xml"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top