Visit all elements in DOM document

This is an example of how to visit all elements in a DOM Document. A DOM is a standard tree structure, where each node contains one of the components from an XML structure. The two most common types of nodes are element nodes and text nodes. Using DOM functions lets you create nodes, remove nodes, change their contents, and traverse the node hierarchy. Visiting all elements in a DOM Document implies that you should:

  • Obtain a new instance of a DocumentBuilderFactory, that is a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
  • Set the parser produced so as not to validate documents as they are parsed, using setValidating(boolean validating) API method of DocumentBuilderFactory, with validating set to false.
  • Create a new instance of a DocumentBuilder, using newDocumentBuilder() API method of DocumentBuilderFactory.
  • Parse the FileInputStream with the content to be parsed, using parse(InputStream is) API method of DocumentBuilder. This method parses the content of the given InputStream as an XML document and returns a new DOM Document object.
  • Get the NodeList of all the Element objects in document order with a given tag name and are contained in the document using getElementsByTagName(String tagname) API method of Document. Since the tag name is set to "*" it matches all tags, so all elements are returned.
  • For each one of the NodeList entries, get each Node cast to Element, using item(int index) API method of NodeList, and get the node name, using getNodeName() API method of Node.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class VisitAllElementsInDOMDocument {
	public static void main(String[] args) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(new FileInputStream(new File("in.xml")));
		NodeList entries = doc.getElementsByTagName("*");
		for (int i=0; i<entries.getLength(); i++) {
		    Element element = (Element) entries.item(i);
		    System.out.println("Found element " + element.getNodeName());



<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
		<title>Java Tutorials and Examples 2</title>
			<title><![CDATA[Java Tutorials 2]]></title>
			<title><![CDATA[Java Examples 2]]></title>


Found element rss
Found element channel
Found element title
Found element language
Found element item
Found element title
Found element link
Found element item
Found element title
Found element link

This was an example of how to visit all elements in a DOM Document in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button