Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM – Navigating Nodes

XML DOM - Navigating Nodes


Nodes can be navigated by using the relationships between nodes.


Navigating DOM Nodes

Accessing nodes in the node tree through the relationships between nodes is usually called navigating nodes ("navigating nodes").

In the XML DOM, node relationships are defined as properties of the nodes:

The following image shows books.xml a part of the node tree, and illustrates the relationships between the nodes:

Node tree


DOM - Parent Nodes

Every node has only one parent. The following code navigates to the parent node of the element:

Example

xmlDoc

=

loadXMLDoc

(

"

books.xml

"

)

;

x

=

xmlDoc

.

getElementsByTagName

(

"

book

"

)

[

0

]

;

document

.

write

(

x

.

parentNode

.

nodeName

)

;

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get the first Element
  3. Output the node name of "x"'s parent node

Avoid Empty Text Nodes

Firefox and some other browsers treat empty whitespace or line breaks as text nodes, while Internet Explorer does not.

This causes a problem when using the following properties: firstChild, lastChild, nextSibling, previousSibling.

To avoid navigating to empty text nodes (spaces and line breaks between element nodes), we use a function to check the node type:



function get_nextSibling(n)

{

  y=n.nextSibling;

  while (y.nodeType!=1)

  {

    y=y.nextSibling;

  }

  return y;

}

The function above allows you to use get_nextSibling (node) instead of nodethe .nextSibling property.

Code Explanation:

The type of an element node is 1. If a sibling node is not an element node, move to the next node until an element node is found. In this way, the same result is obtained in both Internet Explorer and Firefox.


Get the First Child Element

The following code shows the first first element of:

Example

<html>
<head>
<script src="loadxmldoc.js" script>
<script//>

Check whether the first node is an element node

function

get_firstChild

(

n

)

{

y

=

n

.

firstChild

;

while

(

y

.

nodeType

!=

1

)

{

y

=

y

.

nextSibling

;

}

return

y

;

}

script

head

<body>
<script xmlDoc=loadXMLDoc("books.xml" )>

;

x

=

get_firstChild

(

xmlDoc

.

getElementsByTagName

(

"

book

"

)

[

0

]

)

;

document

.

write

(

x

.

nodeName

)

;

script

body

html

Output:

title

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. In the first use the get_firstChild function on the element to get the first child node (which is an element node)
  3. Output the node name of the first child node (which is an element node)

More examples

lastChild()
This example uses the lastChild() method and a custom function to get the last child node of a node

nextSibling()
This example uses the nextSibling() method and a custom function to get the next sibling node of a node

previousSibling()
This example uses the previousSibling() method and a custom function to get the previous sibling node of a node