perplexed by javascript xmlDOM problem

S

Sevinfooter

I'm loading xml files in order to populate some <select> options from
an xml file. i only need to fill these dropdowns on page load, so am
using XML DOM to fill them. Here's basically what the source XML file
looks like, abridged to only one record:

<?xml version="1.0" encoding-"utf-8"?>
<pilots>
<pilot>
<name>some, name</name>
<weight>188</weight>
</pilot>
</pilots>

Now, the following function loads the information ( i have reasons for
only using the microsoft activeXObject as opposed to adding Netscape
functionality):

function loadXML(xmlFile, target) { //target is the name of the
select element
try{
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
} catch (e) {
alert(e);
}
xmlDoc.asyn = "false";
xmlDoc.load(xmlFile);
var xmlObj = xmlDoc.documentElement;
var obj = document.getElementById(target);
var CNodes, textNode, valueNode;
var j = xmlObj.childNodes.length;

CNodes = xmlObj.childNodes;
for (i = 0; i < j; i++) {
textNode = CNodes.firstChild;
valueNode = CNodes.lastChild;

var opt = document.createElement("OPTION");
obj.options.add(opt);
opt.innerText = textNodes.firstChild.nodeValue;
opt.value = valueNode.firstChild.nodeValue;
}
}

This fails at valueNode.firstChild.nodeValue. i have tried adding an
alert with that value in it before the opt.value line, but it fails as
well. the opt.innerText node is fine. Any ideas why this dies there?
 
M

Martin Honnen

Sevinfooter wrote:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
} catch (e) {
alert(e);
}
xmlDoc.asyn = "false";

The property name is async and not asyn, and you should assign a boolean
property and not a string
xmlDoc.async = false;
And that setting blocks the browser when the file is being loaded and
parsed so in a production environment you should rather use
xmlDoc.async = true;
and use an event handler to be fired when the document is ready.

textNode = CNodes.firstChild; ^^^^^^^^

opt.innerText = textNodes.firstChild.nodeValue;

^^^^^^^^^

That can't work.


Why do you bother with childNodes if there are methods like
getElementsByTagName (and with MSXML which you use)
selectNodes/selectSingleNode?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top