By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

perplexed by javascript xmlDOM problem

P: n/a
I'm loading xml files in order to populate some <selectoptions 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[i].firstChild;
valueNode = CNodes[i].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?

Jul 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a


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[i].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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.