w3schools    w3Schools
Search W3Schools :
   
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
ADVERTISEMENTS

XML Certification
Download XML editor
Custom Programming
 
Table of contents
XML DOM Tutorial
DOM HOME
DOM Introduction
DOM Nodes
DOM Node Tree
DOM Parsing
DOM Load Function
DOM Methods
DOM Accessing
DOM Node Info
DOM Node List
DOM Traversing
DOM Browsers
DOM Navigating

Manipulate Nodes
DOM Get Values
DOM Change Nodes
DOM Remove Nodes
DOM Replace Nodes
DOM Create Nodes
DOM Add Nodes
DOM Clone Nodes
DOM HttpRequest

XML DOM Reference
DOM Node Types
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentImpl
DOM DocumentType
DOM ProcessingInstr
DOM Element
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError Obj
DOM Parser Errors

DOM Summary

Examples
DOM Examples
DOM Validator

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Tutorials
W3Schools Forum

Helping W3Schools

 

XML DOM - Accessing Nodes

previous next

With the DOM, you can access every node in an XML document.


Examples

The examples below use the XML file books.xml.
A function, loadXMLDoc(), in an external JavaScript is used to load the XML file.

Access a node using its index number in a node list
This example uses the getElementsByTagname() method to get the third <title> element in "books.xml"

Loop through nodes using the length property
This example uses the length property to loop through all <title> elements in "books.xml"

See the node type of an element
This example uses the nodeType property to get node type of the root element in "books.xml".

Loop through element nodes
This example uses the nodeType property to only process element nodes in "books.xml".

Loop through element nodes using node realtionships
This example uses the nodeType property and the nextSibling property to process element nodes in "books.xml".


Accessing Nodes

You can access a node in three ways:

1. By using the getElementsByTagName() method

2. By looping through (traversing) the nodes tree.

3. By navigating the node tree, using the node relationships.


The getElementsByTagName() Method

getElementsByTagName() returns all elements with a specified tag name.

Syntax

node.getElementsByTagName("tagname");

Example

The following example returns all <title> elements under the x element:

x.getElementsByTagName("title");

Note that the example above only returns <title> elements under the x node. To return all <title> elements in the XML document use:

xmlDoc.getElementsByTagName("title");

where xmlDoc is the document itself (document node).


DOM Node List

The getElementsByTagName() method returns a node list. A node list is an array of nodes.

The following code loads "books.xml" into xmlDoc using loadXMLDoc() and stores a list of <title> nodes (a node list) in the variable x:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");

The <title> elements in x can be accessed by index number. To access the third <title> you can write:

y=x[2];

Try it yourself.

Note: The index starts at 0.

You will learn more about node lists in a later chapter of this tutorial.


DOM Node List Length

The length property defines the length of a node list (the number of nodes).

You can loop through a node list by using the length property:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
  { 
  document.write(x[i].childNodes[0].nodeValue);
  document.write("<br />");
  }

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Get all <title> element nodes
  3. For each title element, output the value of its text node

Try it yourself.


Node Types

The documentElement property of the XML document is the root node.

The nodeName property of a node is the name of the node.

The nodeType property of a node is the type of the node.

You will learn more about the node properties in the next chapter of this tutorial.

Try it yourself.


Traversing Nodes

The following code loops through the child nodes, that are also element nodes, of the root node:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;

for (i=0;i<x.length;i++)
{ 
  if (x[i].nodeType==1)
  {//Process only element nodes (type 1) 
  document.write(x[i].nodeName);
  document.write("<br />");
  } 
}

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Get the child nodes of the root element
  3. For each child node, check the node type of the node. If the node type is "1" it is an element node
  4. Output the name of the node if it is an element node

Try it yourself.


Navigating Node Relationships

The following code navigates the node tree using the node relationships:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;

for (i=0;i<x.length;i++)
{
if (y.nodeType==1)
  {//Process only element nodes (type 1)
  document.write(y.nodeName + "<br />");
  }
y=y.nextSibling;
}
  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Get the child nodes of the first book element
  3. Set the "y" variable to be the first child node of the first book element
  4. For each child node (starting with the first child node "y"):
  5. Check the node type. If the node type is "1" it is an element node
  6. Output the name of the node if it is an element node
  7. Set the "y" variable to be the next sibling node, and run through the loop again

Try it yourself.


previous next


Make your web applications look like a million bucks

FusionCharts   

Most web applications today use boring methods to present data to their viewers using grids or simple HTML tables. FusionCharts induces "life" into the web applications by converting monotonous data into lively charts, gauges & maps.

FusionCharts works with all technologies like ASP, ASP.NET, PHP, ColdFusion, Ruby on Rails, JSP, HTML pages etc. and connects to any database to render animated & interactive charts. It takes less than 15 minutes and no expertise whatsoever to build your first chart and just a glance of it to captivate your audience. This fact is endorsed by our 12,000 customers and 150,000 users which include a majority of the Fortune 500 companies. And yeah, your applications could look like a million bucks by spending just $69.

So go ahead, download your copy of FusionCharts and start "wow-ing" your customers now!


 
WEB HOSTING
Shopping Cart
ASP.NET
Shopping Cart
$15 Domain Name
Registration
Save $20 / year!
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
Home HOME or Top of Page Validate   Validate   W3C-WAI level A conformance icon Printer Friendly  Printer Friendly

W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2009 by Refsnes Data. All Rights Reserved.