Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM access nodes


May 27, 2021 XML DOM


Table of contents


XML DOM - Access node


With DOM, you have access to each node in the XML document.

DoM accesses nodes in three ways: using the getElementsByTagName() method, looping (traversing) the node tree, and navigating the node tree using the node's relationships


XML DOM access nodes

Try it - instance

The following example uses the XML file .xml.
The function loadXMLDoc(), located in an external JavaScript, is used to load XML files.

Use the index number in the node list to access the node
This example uses the getElementsByTagname() method to get .xml element in the "books".

Use the length property to traverse the node
This example uses the length property to traverse all the elements .xml the "books" and "books".

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

Traverse element nodes
This example uses the nodeType property to handle .xml nodes in "books and data."

Use the relationship of the node to traverse the element node
This example uses the nodeType property and the nextSibling property to handle .xml nodes in "books."


Access the node

You can access nodes in three ways:

1. By using the getElementsByTagName() method.

2. Cycle (traverse) the node tree.

3. Navigate through the node tree by taking advantage of the node's relationship.


GetElements ByTagName() method

GetElementsByTagName() returns all elements that have the specified label name.

Grammar

node .getElementsByTagName( "tagname" );

Instance

The following example returns all the elements under the x element:

x.getElementsByTagName("title");

Note that the above instance returns only the element under the x node. To return all the elements in the XML documentation, use:

xmlDoc.getElementsByTagName("title");

Here, xmlDoc is the document itself (document node).


List of DOM nodes (Node List)

The getElements ByTagName() method returns a list of nodes. The list of nodes is an array of nodes.

The following code uses loadXMLDoc() to load "books.xml" into xmlDoc, and then stores a list of nodes in the variable x:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");

The element in x is accessible through the index number. To access the third slt;title, you can write:

y=x[2];

Note: The index starts at 0.

Later in this tutorial, you'll learn more about node lists.


DoM Node List Length (Node List Length)

The length property defines the length of the list of nodes (that is, the number of nodes).

You can traverse the list of nodes 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("
");
}

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets all the element nodes
  3. Outputs the value of the text node for each of the elements

Node Types

DocumentElement property stone root node of the XML document.

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

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

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

Give it a try


Traverse the node

The following code traverses the child nodes of the root node, which are also element nodes:

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("
");
}
}

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets the child node of the root element
  3. Check the node type for each child node. If the node type is "1", it is an element node
  4. If it is an element node, the name of the output node

The relationship between the navigation nodes

The following code uses node relationships to navigate the node tree:

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 + "
");
}
y=y.nextSibling;
}

Try it out . . .
  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets the child node of the first book element
  3. Set the "y" variable to the first child node of the first book element
  4. For each child node (the first child node starts with "y"), check the node type, which is the element node if the node type is "1"
  5. If it is an element node, output the name of the node
  6. Set the "y" variable to the next peer node and run the loop again