getAttribute problem when using XML with JavaScript

M

mr_burns

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>
 
M

Martin Honnen

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');
 

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

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top