471,582 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,582 software developers and data experts.

getAttribute problem when using XML with JavaScript

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
1 4658


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.

Similar topics

6 posts views Thread by Banski | last post: by
6 posts views Thread by vivekian | last post: by
3 posts views Thread by gigs | last post: by
4 posts views Thread by RobG | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.