Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Dom Node List in XML

Dom Node List in XML

DOM NodeList in XML

A NodeList is a collection (like an array) of nodes returned by DOM methods such as:

  • getElementsByTagName()

  • childNodes

  • querySelectorAll() (in HTML DOM)


What is a NodeList?

  • It is array-like, meaning you can access items by index: nodeList[0].

  • It is live or static depending on the method (e.g., getElementsByTagName returns a live NodeList, reflecting changes in the DOM).

  • You can loop over a NodeList using for loop or for...of (in modern browsers).


Example XML

<library>  <book id="101"><title>Effective Java</title></book>  <book id="102"><title>Clean Code</title></book>  <book id="103"><title>JavaScript: The Good Parts</title></book></library>

Getting a NodeList in JavaScript

// Assuming xmlDoc is the parsed XML documentconst books = xmlDoc.getElementsByTagName("book"); // NodeList of all <book> elementsconsole.log(books.length);  // Number of <book> nodes// Access individual nodesconst firstBook = books[0];console.log(firstBook.getAttribute("id"));  // "101"

Looping Through a NodeList

for (let i = 0; i < books.length; i++) {  const book = books[i];  const title = book.getElementsByTagName("title")[0].textContent;  console.log(`Book ID: ${book.getAttribute("id")}, Title: ${title}`);}

Or using modern syntax:

for (const book of books) {  const title = book.getElementsByTagName("title")[0].textContent;  console.log(`Book ID: ${book.getAttribute("id")}, Title: ${title}`);}

Important Notes

  • NodeList is not exactly an Array, so array methods like .map(), .filter() won’t work unless you convert it:

const booksArray = Array.from(books);booksArray.forEach(book => {  console.log(book.getAttribute("id"));});
  • In older browsers, you may need for loops instead of for...of.


Summary

MethodReturnsDescription
getElementsByTagName()NodeList (live)All elements with given tag name
childNodesNodeList (live)All child nodes (elements, text, comments)
querySelectorAll()NodeList (static)All elements matching selector

If you want, I can provide a runnable HTML + JS example demonstrating NodeList usage on XML!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql