/    Sign up×
Community /Pin to ProfileBookmark

Reading dynamic XML in JavaScript for Mozilla

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… [B]I’m already familiar with loading and parsing XML in ActionScript and classic ASP, so the basics haven’t been that difficult.[/B]

[B]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. [/B]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);
}
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@jwalbornauthorFeb 06.2008 — Just an FYI, this was indented before I pasted it in here... I'm having trouble getting answers to this, so I'm posting it to as many forums as I can find. I have a serious need for a Mac-compatible version of this software post-haste. Sorry I don't have the time to put the indents back in.
Copy linkTweet thisAlerts:
@jwalbornauthorFeb 06.2008 — 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>
[/CODE]
Copy linkTweet thisAlerts:
@jwalbornauthorFeb 06.2008 — ...so I have to post a new message to get an indented version of the code available for you guys.... Sorry for spamming my own topic. :rolleyes:

<i>
</i> var xmlPath = "GUI_GET_AssetLocations.asp"

<i> </i> //(loadXML function called on page load)

<i> </i> function loadXML()
<i> </i> {
<i> </i> // Check for Mozilla browser type
<i> </i> if (document.implementation &amp;&amp; document.implementation.createDocument)
<i> </i> {
<i> </i> // Build XMLDOM object and establish "onload" event for Mozilla
<i> </i> xmlDoc = document.implementation.createDocument("", "", null);
<i> </i> xmlDoc.ignorewhitespace = true;
<i> </i> xmlDoc.onload = display;
<i> </i> }
<i> </i> // Check for Internet Explorer browser type
<i> </i> else
<i> </i> {
<i> </i> // Build XMLDOM object and establish "onload" event for MSIE
<i> </i> xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
<i> </i> xmlDoc.onreadystatechange = function ()
<i> </i> {
<i> </i> if (xmlDoc.readyState == 4) display()
<i> </i> };
<i> </i> }
<i> </i> // Load XML based on passed path
<i> </i> xmlDoc.load(xmlPath);
<i> </i> }

<i> </i> function display()
<i> </i> {
<i> </i> //
<i> </i> var xmlTags = xmlDoc.getElementsByTagName('location');
<i> </i> //
<i> </i> for (var i=0;i&lt;xmlTags.length;i++)
<i> </i> {
<i> </i> //
<i> </i> var nodeLoop = xmlTags[i].childNodes.length;
<i> </i> //
<i> </i> for (var ii=0; ii&lt;nodeLoop; ii++)
<i> </i> {
<i> </i> //
<i> </i> var nodeContents = xmlTags[i].childNodes[ii];
<i> </i> //
<i> </i> var nodeLabel = nodeContents.nodeName;
<i> </i> //
<i> </i> if (nodeContents.childNodes.length &gt; 0)
<i> </i> {
<i> </i> var nodeData = nodeContents.firstChild.nodeValue;
<i> </i> } else {
<i> </i> var nodeData = ""
<i> </i> }
<i> </i> //
<i> </i> if (nodeLabel == "id")
<i> </i> {
<i> </i> var dataId = nodeData
<i> </i> }
<i> </i> //
<i> </i> if (nodeLabel == "name")
<i> </i> {
<i> </i> var dataName = nodeData
<i> </i> }
<i> </i> //
<i> </i> if (nodeLabel == "lat")
<i> </i> {
<i> </i> var dataLat = nodeData
<i> </i> }
<i> </i> //
<i> </i> if (nodeLabel == "long")
<i> </i> {
<i> </i> var dataLong = nodeData
<i> </i> }
<i> </i> //
<i> </i> if (nodeLabel == "almstattxt")
<i> </i> {
<i> </i> var dataAlarm = nodeData
<i> </i> }
<i> </i> //
<i> </i> if (nodeLabel == "icon")
<i> </i> {
<i> </i> var dataIcon = customIconPre + nodeData + customIconSuf
<i> </i> }
<i> </i> }
<i> </i> //
<i> </i> AddPin(dataId, dataName, dataLat, dataLong, dataAlarm, dataIcon);
<i> </i> //
<i> </i> //alert(dataId + ";" + dataName + ";" + dataLat + ";" + dataLong + ";" + dataAlarm);
<i> </i> }
<i> </i> }
Copy linkTweet thisAlerts:
@jwalbornauthorFeb 07.2008 — 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):

<i>
</i>Response.ContentType="text/xml"
×

Success!

Help @jwalborn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...