Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3725066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:52:11+00:00 2026-05-19T06:52:11+00:00

Is there a way to loop through the immediate children of an XML node

  • 0

Is there a way to loop through the immediate children of an XML node in JavaScript, without using jquery or a similar library? I tried to use “.childNodes”, but for some reason it doesn’t work as it should. “.childNodes.length” return a number which is usually greater than the number of immediate nodes, and all of the tag names (using .tagName) are for some reason undefined. I know that my XML data is formated correctly because if I call “.getElementsByTagName()” using the tags of the immediate children, it works as it should.
Some examples of my dilemma :

var root = xmlData.getElementsByTagName("library_geometries")[0]; 

for (i = 0; i < root.childNodes.length; i++) //get all the geometries
{  
 geom =  root.childNodes[i];  
 alert(geom.tagName);
}

------------------------------------------------------
geom = root.getElementsByTagName("geometry");

for (i = 0; i < geom.length; i++) //get all the geometries
{  
 alert(geom[i].tagName);
}

First one doesn’t work at all, second one works in this example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T06:52:13+00:00Added an answer on May 19, 2026 at 6:52 am

    This is actually a clarification of Hemlock’s answer. I’m putting it here instead of commenting his answer because I don’t have space to draw pretty ASCII art in comments.

    Lets say we have the following XML:

    <a><b></b><c></c></a>
    

    This generates the following DOM:

    <a>--.
         |
        <b>
         |
        <c>
    

    which is generally what you’d expect.

    Lets say we now have the following XML:

    <a>
       <b></b>
       <c></c>
    </a>
    

    You would think this generates the same DOM. But according to the standard, you’d be wrong. Instead the standard requires it to generate the following DOM:

    <a>--.
         |
        "\n   "
         |
        <b>
         |
        "\n   "
         |
        <c>
         |
        "\n"
    

    Yes, the spec says all those whitespace should be captured in the DOM. Almost all XML implementations out there does this (not only in browsers). The only exception being IE (and by extension the XML engine in JScript) because Microsoft didn’t care much about violating standards.

    Personally this is useless 99.999% of the time. About the only time this would be useful is if you’re trying to implement an XML code editor. But it’s in the standards so browsers need to implement it if they want to be standards compliant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use a foreach loop to iterate through a collection
Is there any way to clean up this type of loop using LINQ? List<Car>
Is there any way to check whether a file is locked without using a
Is there a way to loop through all the properties from within a class's
Is there a way to use THE LOOP in Wordpress to load pages instead
Is there a way in Java's for-each loop for(String s : stringArray) { doSomethingWith(s);
Within a Foreach loop in SSIS is there a way that if a task
Is there a way to select all CSS rules of an element with JavaScript?
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.