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

getAttribute problem when using XML with JavaScript

P: n/a
Hi,

I have the following code that works fine except the getAttribute line:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note.xml");
nodes = xmlDoc.documentElement.childNodes;
text = '<p>note:<br>';
for (var i=0; i<nodes.length; i++) {
title = nodes.item(i).firstChild.text;
value = nodes.item(i).firstChild.getAttribute('id');
text+= title+':'+value;
}
text+= '</p>';
an example of the xml nodes are:
<?xml...
<all>
<node id="1">text</node>
<node id="2">text</node>
<node id="3">text</node>
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


mr_burns wrote:

I have the following code that works fine except the getAttribute line:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note.xml");
nodes = xmlDoc.documentElement.childNodes;
text = '<p>note:<br>';
for (var i=0; i<nodes.length; i++) {
title = nodes.item(i).firstChild.text;
value = nodes.item(i).firstChild.getAttribute('id');


Well what exactly goes wrong? Do you get an error? Or is the result not
what you expect?
In the DOM object model not every child node needs to be an element
node, and only element nodes implement the getAttribute.
What you are trying to do is access a child node of an element node and
in your example those child nodes are text nodes and thus do not have a
method getAttribute.
With MSMXL you will probably get the result you are looking for if you
change your code to use the lines
title = nodes.item(i).text;
value = nodes.item(i).getAttribute('id');

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.