How to Parse XML Data in Java Using DOM Parser

When working with applications that require structured data — like employee records, product catalogs, or configuration files — XML (eXtensible Markup Language) becomes a go-to format. In Java, one of the most widely-used ways to read and manipulate XML is through the DOM Parser.

In this tutorial, we’ll learn:

  • What the DOM Parser is.

  • When and why to use it.

  • How to parse an XML file step-by-step using Java DOM Parser.

  • And we’ll walk through a practical example with code and explanation.

 What is DOM Parser?

DOM stands for Document Object Model. A DOM Parser in Java reads the entire XML document and loads it into memory as a tree structure. This tree allows easy access to any part of the document. Each tag, attribute, or text is represented as a node.

Best suited for: Small to medium-sized XML files where full access or modifications to the data are required.

When to use DOM Parser?

  • When you need to read and write data from an XML file.

  • When the XML document is small to medium-sized (fits comfortably in memory).

  • When you need to navigate the entire XML tree, including parent-child relationships.

  • When you want to modify, delete, or add nodes dynamically.

Why to use DOM Parser

  • Loads the entire XML file into a tree structure, making it easy to traverse.

  • Offers random access to any element or attribute in the document.

  • Beginner-friendly due to its object-oriented, hierarchical approach.

  • Supports modifying the XML structure before writing it back to the file.

NOTE : DOM parser is ideal when you need full control over the XML data and your document isn’t too large to fit in memory.

Sample XML File (finTest.xml)

We will be using  XML File that stores information of the employees.

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <employees>
    <employee id = "1">
      <name>John Doe</name>
      <position>Software Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id = "2">
      <name>Steven Broad</name>
      <position>IT Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id ="3">
      <name>Jane Warner</name>
      <position>Software Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id="4">
      <name>Ben Doe</name>
      <position>Software Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id ="5">
      <name>John Smith</name>
      <position>IT Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id="6">
      <name>Ben Marsh</name>
      <position>Software Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id="7">
      <name>Jonahan</name>
      <position>Software Engineer</position>
      <department>Engineering</department>
    </employee>

    <employee id="8">
      <name>Alex Carry</name>
      <position>Software Engineer</position>
      <department>Engineering</department>
    </employee>
  </employees>
  
</company>

Here<company> root node and <employees>  is child node of <company> .

Each <employee> node has:

  • An id attribute

  • A <firstName> child element

  • A <lastName> child element

Code:

package DomParser;

import org.w3c.dom.*;

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

public class Parser {

    public static void main(String[] args) {
        try {
            // Step 1: Load and parse the XML document
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("finTest.xml");

            // Normalize document (merges adjacent text nodes, removes whitespace noise)
            document.getDocumentElement().normalize();

            // Step 2: Access the root element
            Element root = document.getDocumentElement();
            System.out.println("Root Node: " + root.getNodeName());

            NodeList rootChildren = root.getChildNodes();
            for (int i = 0; i < rootChildren.getLength(); i++) {
                Node child = rootChildren.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    System.out.println("Child Node of Root: " + child.getNodeName());
                }
            }

            System.out.println("\n--- Employee Details ---");

            // Step 3: Parse each <employee> node
            NodeList employeeList = document.getElementsByTagName("employee");
            for (int i = 0; i < employeeList.getLength(); i++) {
                Element employee = (Element) employeeList.item(i);
                String id = employee.getAttribute("id");

                System.out.println("Employee ID: " + id);

                NodeList childNodes = employee.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node child = childNodes.item(j);
                    if (child.getNodeType() == Node.ELEMENT_NODE) {
                        Element detail = (Element) child;
                        String tag = capitalize(detail.getTagName());
                        String value = detail.getTextContent().trim();
                        System.out.println("  " + tag + ": " + value);
                    }
                }

                System.out.println("--------------------------");
            }

        } catch (Exception e) {
            System.err.println("Error parsing XML: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * Capitalizes the first letter of the given string.
     */
    private static String capitalize(String str) {
        if (str == null || str.isEmpty()) return str;
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}

Output:

Root Node: company
Child Node of Root: employees

--- Employee Details ---
Employee ID: 1
Name: John Doe
Position: Software Engineer
Department: Engineering
--------------------------
Employee ID: 2
Name: Steven Broad
Position: IT Engineer
Department: Engineering
--------------------------
Employee ID: 3
Name: Jane Warner
Position: Software Engineer
Department: Engineering
--------------------------
Employee ID: 4
Name: Ben Doe
Position: Software Engineer
Department: Engineering
--------------------------
Employee ID: 5
Name: John Smith
Position: IT Engineer
Department: Engineering
--------------------------
Employee ID: 6
Name: Ben Marsh
Position: Software Engineer
Department: Engineering
--------------------------
Employee ID: 7
Name: Jonahan
Position: Software Engineer
Department: Engineering
--------------------------
Employee ID: 8
Name: Alex Carry
Position: Software Engineer
Department: Engineering
--------------------------


Explanation:

Step 1: Setup the Parser

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

This prepares the DOM parser engine.

Step 2: Load and Parse XML

Document document = builder.parse(new File("finTest.xml"));
document.getDocumentElement().normalize();

Loads the XML into memory and converts it to a normalized tree.

 Step 3: Access Elements

NodeList nodeList = document.getElementsByTagName("employee");

Gets a list of all <employee> nodes.

 Step 4: Read Attributes and Child Tags

String id = element.getAttribute("id");
String firstName = element.getElementsByTagName("firstName").item(0).getTextContent();
String lastName = element.getElementsByTagName("lastName").item(0).getTextContent();

Reads the employee’s ID, first name, and last name.

When Not to Use DOM Parser

  • When working with very large XML files (it loads entire file in memory)

  • In cases where you only need to read a small portion of the document (use StAX or SAX instead)

Conclusion

The DOM parser in Java is a powerful and simple tool for reading and modifying XML documents. For small to mid-sized XML files, it offers a great mix of ease-of-use and flexibility.

Check out my some other tutorials:

How to Create Custom Annotations in Java (With Real-World Example)

Encryption and Decryption Of Data in Java Using AES Algorithm

Read and Write Files in Java Using FileReader and FileWriter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top