473,382 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 4724


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hutch | last post by:
Hi, I'm making use of this VERY useful function for a so called 'standards compliant' replacement for target="_blank": function externalLinks() { if (!document.getElementsByTagName) return;...
6
by: Banski | last post by:
Hi, Im quite new to XML in .Net. Im getting values from an xml file using XPath and sorting as you see in the code below. I cant figure out how to get the value of date with GetAttribute. Hope...
3
by: duff | last post by:
Hiya, I'm trying to replace the commented out code below with a neater version using the IAttributeAccessor interface (so that I don't have to write 'if' statements for all control types). At...
0
by: sandy | last post by:
I have a AxWebBrowser class. I wish to locate a specific element in its HTMLDocument which has a specific 'style'. I use the following code to access the element with the specific style:- ...
6
by: vivekian | last post by:
Hi , In one of the javascript functions , i need to get the ids of the elements. This function works in Internet Explorer : var TabNames = ParentTab.childNodes ; for ( i = 0 ; i <...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
3
by: gigs | last post by:
i have XpathNodeIterator iterator. and i have some "od" attributes when i took that nodes with that attribute i dont get anything when i call iterator.Current.GetAttribute("od", "") people...
4
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of...
3
by: maminx | last post by:
I have this script... var td = document.createElement('td'); var p = document.createElement('p'); var label = document.createElement('label'); var span =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.