473,324 Members | 2,214 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,324 software developers and data experts.

childNodes.length problem

Hi all,

I have a problem with getting the amount of childs in a XML structure,
the strucure is somewhat like the following:

<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>
<xmlnames>
<field>id</field>
<field>name</field>
<field>availablefrom</field>
<field>availableuntill</field>
<field>owner</field>
</xmlnames>
</cms>

If I use the following to get te amount of childs for xmlnames I get an
amount of 11:
xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].childNodes.length

Is this wrong or maybe there is a better / other way of counting the
child amount?

Regards,

Arno

Jan 4 '06 #1
3 8444
Q1tum wrote:
Hi all,

I have a problem with getting the amount of childs in a XML structure,
the strucure is somewhat like the following:

<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>
<xmlnames>
<field>id</field>
<field>name</field>
<field>availablefrom</field>
<field>availableuntill</field>
<field>owner</field>
</xmlnames>
</cms>

If I use the following to get te amount of childs for xmlnames I get an
amount of 11:
xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].childNodes.length
Which seems correct to me. Empty text nodes will be inserted at the
end of each line where there is a new line or line feed character, so
there are empty text nodes after the opening 'xmlnames' tag and the 5
closing 'field' tags that are all children of the xmlnames element.

That's 6 empty text nodes plus the 5 'field' child nodes, making 11.


Is this wrong
No.

or maybe there is a better / other way of counting the
child amount?


If you want to get just the children with a tagName of 'field', then
use getElementsByTagName(). Note that will get all the descendants of
xmlnames that have a tag called 'field', not just the direct children.

If you want explicitly the children, then use a loop to get them:

var t = xmlObj.responseXML.getElementsByTagName('xmlnames' );
var c = t[0].childNodes;
var k = [];
for (var i=0, len=c.length; i<len; ++i){
if('field' == c[i].tagName){
k[k.length] = c[i];
}
}

// k is now an array of the child nodes of xmlnames that are
// field nodes.

--
Rob
Jan 4 '06 #2


Q1tum wrote:

<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>
<xmlnames>
<field>id</field>
<field>name</field>
<field>availablefrom</field>
<field>availableuntill</field>
<field>owner</field>
</xmlnames>
</cms>

If I use the following to get te amount of childs for xmlnames I get an
amount of 11:
xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].childNodes.length

Is this wrong or maybe there is a better / other way of counting the
child amount?


childNodes contains all kind of nodes and in your case there a text
nodes with white space between the element nodes e.g. you have

<xmlnames>^^^^^white space text node here
^^^^^ <field>id</field>^^^^ and here
^^^^^ <field>name</field>^^^^ and here ^^^^
</xmlnames>

and not

<xmlnames><field>id</field><field>name</field></xmlnames>

If you are looking only for element nodes then depending on what you
need and depending on the structure of the XML you need to loop through
childNodes and check for nodeType == 1 or you can use
getElementsByTagName('*') or in your case simply

xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].getElementsByTagName('field')

Be aware that getElementsByTagName finds all descendant elements so with
nested structures it is not equivalent to childNodes and checking
nodeType == 1.

But in many cases including your example file when people run into white
space text nodes issues with childNodes then simply switching to
getElementsByTagName to find the elements needed works without problems.

Besides that there is also XPath and NodeFilter/TreeWalker APIs but
support for that is not as wide spread as childNodes/nodeType and
getElementsByTagName and XPath for instance is supported in MSXML 3 and
later for IE/Win and in Mozilla but the APIs are very different.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 4 '06 #3
Thank you,

your script works fine!

Martin Honnen thanks as well for explaining

Jan 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: adam | last post by:
i'm working on a portion of a CMS that allows content-admins to browse a product list, and add individual products into the taxonomy by clicking checkboxes next to categories they might belong in....
6
by: Ron Brennan | last post by:
Good evening. <span id=fileList> <input type=hidden name=file id=file_0 value=Name/> </span> .... document.getElementById(fileList).childNodes The childNodes is giving me two children: 1)...
2
by: chuck | last post by:
Hi, I am modifying some code from here http://www.quirksmode.org/dom/domform.html I have a div 'readroot' that I clone. I change the change the id and name of the childnodes of 'readroot' to...
3
by: Jake Barnes | last post by:
This weekend I decided to play around with Javascript a little and try to teach myself some things about AJAX and DOM. I've been doing experiments on this page: ...
12
by: Q1tum | last post by:
Hi all, I got a problem with childNodes.length, I use the following XML for my guestbook: <?xml version="1.0" encoding="ISO-8859-1"?> <guestbook> <entry> <from>Q1tum</from>...
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
5
by: Moses | last post by:
HI The Value for childNodes.length differs with mozilla and IE Is it problem with my coding..... I could not under stood............. The following is the details
2
by: windandwaves | last post by:
Hi Folk I want to add an "onClick" function to the radio boxes, but I am having trouble. Can you help me <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://...
2
by: willyWEB66 | last post by:
Hi everyone, I have this code in javascript where the XML file is loaded and displayed to an html using XSLT. It works fine in IE but not in Firefox. My problem is in the looping to the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.