Read and Parse XML file using Java DOM Parser [Java Tutorial]


In this tutorial we will see how to read and parse an XML (eXtensible Markup Language) file using Java code following the DOM (Document Object Model) Parser under javax.xml.parsers.

❗️ Be aware that DOM XML Parser in Java does consume a lot of memory resources as it creates a Document Object of the entire XML in the memory (RAM) and hence it is also slow. You should be using DOM parser only when the XML file size is relatively less. If you have a huge file you must use the SAX Parser is recommended.

Sample XML file to be parsed using DOM parser

    <?xml version="1.0"?>
    <Countries>
        <Country>
            <name>United States</name>
            <alpha_code>USA</alpha_code>
            <population>328,239,523</population>
            <primary_language>English</primary_language>
        </Country>
        <Country>
            <name>Australia</name>
            <alpha_code>AUS</alpha_code>
            <population>25,019,600</population>
            <primary_language>English</primary_language>
        </Country>
        <Country>
            <name>New Zealand</name>
            <alpha_code>NZL</alpha_code>
            <population>4,893,830</population>
            <primary_language>English</primary_language>
        </Country>
        <Country>
            <name>United Kingdom</name>
            <alpha_code>GBR</alpha_code>
            <population>66,040,229</population>
            <primary_language>English</primary_language>
        </Country>
        <Country>
            <name>Ireland</name>
            <alpha_code>IRL</alpha_code>
            <population>4,900,000</population>
            <primary_language>English</primary_language>
        </Country>
        <Country>
            <name>Canada</name>
            <alpha_code>CAN</alpha_code>
            <population>35,985,751</population>
            <primary_language>English</primary_language>
        </Country>
    </Countries>

Step-by-Step: Writing Java code to praise XML file using DOM Parser

    Step 1: Create a Java Class say MyXmlDomParserExample .java with main method

    As in this tutorial we will be using just one class with main() to demonstrate dom parsing, lets create the initial skeleton of the class,

    package com.code2care.tutorials;
    
    public class MyXmlDomParserExample {
    
    	public static void main(String argv[]) {
    	}
    }

Step 2: Create a File object to read our XML file

    String xmlFilePathname = "countries.xml";
    File xmlFile = new File(xmlFilePathname);

    Now let's create a File object using the constructor java.io.File.File(String pathname) in the main method, depending upon where the file is located you need to provide the absolute path of the file (Windows/macOS), for simplicity I have placed the file under my Java Project,

    ⛏️ You need to import: import java.io.File;

Step 3: Get an instance of DocumentBuilderFactory from javax.xml.parsers using the static method newInstance(),

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilderFactory: This class defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. It was added to Java version 1.4.

    ⛏️ You need to import: import javax.xml.parsers.DocumentBuilderFactory;

Step 4: Create a DocumentBuilder object using the documentBuilderFactory.newDocumentBuilder() method,

    DocumentBuilder documentBuilder =  documentBuilderFactory.newDocumentBuilder();

    Now we create a new instance of a javax.xml.parsers.DocumentBuilderusing the currently configured parameters. Note newDocumentBuilder() method throws ParserConfigurationException, so either handle this by surrounding this code under try/catch block or let our main method throws this checked exception.

    ⛏️ You need to do the below imports:
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.ParserConfigurationException;

Step 5: Create org.w3c.dom.Document object using documentBuilder.parse(File filename);

    Document document = documentBuilder.parse(xmlFile);

    Document is an interface that renders the entire HTML or XML document. You can say it holds the root of the document tree and provides primary access to the data within the XML/HTML document.

    This Document interface also includes the factory methods that are needed to create the objects of XML elements, text nodes, comments, etc.

    The parse() parses the content of the XML document and returns a new DOM Document object, it throws exceptions like SAXException, IOException, and IllegalArgument.

Step 6: Create org.w3c.dom.NodeList object for the node we need to fetch data

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

    The NodeList is an Interface that provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live. The items in the NodeList are accessible via an integral index, starting from 0.

    ⛏️ You need to do the imports: newDocumentBuilder

Step 7: Iterating and parsing XML Node Elements

    for (int i = 0; i <= nodeList.getLength() - 1; i++) {
    
    	Node node = nodeList.item(i);
    	Element element = (Element) node;
    
    	System.out.println("---- Element: " + (i + 1) + " ----");
    	System.out.println("Country Name: " + getElementText(element, "name"));
    	System.out.println("Alfa Code: " + getElementText(element, "alpha_code"));
    	System.out.println("Population: " + getElementText(element, "population"));
    	System.out.println("Primary Language: " + getElementText(element, "primary_language"));
    
    }
    public static String getElementText(Element element, String tagName) {
    	return element.getElementsByTagName(tagName).item(0).getTextContent();
    }

    ⛏️ Imports:
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;

Complete Code:
package com.code2care.tutorials;

import java.io.File;
import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 * This is an tutorial to demonstrate
 * Parsing XML file using Java
 * XML DOM Parser.
 * 
 * @author Code2care Date: 11/07/2020
 *
 */
public class MyXmlDomParserExample {

	public static void main(String argv[]) 
			throws ParserConfigurationException, SAXException, IOException {

		String xmlFilePathname = "countries.xml";
		File xmlFile = new File(xmlFilePathname);

		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.parse(xmlFile);

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

		for (int i = 0; i <= nodeList.getLength() - 1; i++) {

			Node node = nodeList.item(i);
			Element element = (Element) node;

			System.out.println("---- Element: " + (i + 1) + " ----");
			System.out.println("Country Name: " + getElementText(element, "name"));
			System.out.println("Alfa Code: " + getElementText(element, "alpha_code"));
			System.out.println("Population: " + getElementText(element, "population"));
			System.out.println("Primary Language: " + getElementText(element, "primary_language"));

		}
		
	}

	public static String getElementText(Element element, String tagName) {
		return element.getElementsByTagName(tagName).item(0).getTextContent();
	}

}
Output:
---- Element: 1 ----
Country Name: United States
Alfa Code: USA
Population: 328,239,523
Primary Language: English
---- Element: 2 ----
Country Name: Australia
Alfa Code: AUS
Population: 25,019,600
Primary Language: English
---- Element: 3 ----
Country Name: New Zealand
Alfa Code: NZL
Population: 4,893,830
Primary Language: English
---- Element: 4 ----
Country Name: United Kingdom
Alfa Code: GBR
Population: 66,040,229
Primary Language: English
---- Element: 5 ----
Country Name: Ireland
Alfa Code: IRL
Population: 4,900,000
Primary Language: English
---- Element: 6 ----
Country Name: Canada
Alfa Code: CAN
Population: 35,985,751
Primary Language: English
Tutorial Java XML DOM Parser

Troubleshooting Errors while Parsing XML using DOM Parser

1) java.io.FileNotFoundException

If the XML file is not accessible, then you will get java.io.FileNotFoundException exception. Make sure the file is present in the location provided and the file name is correct.

Exception in thread "main" java.io.FileNotFoundException: C:\eclipse-workspace\counties.xml 
(The system cannot find the file specified)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
	at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
	at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
	at java.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:652)
	at java.xml/impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:150)
	at java.xml/internal.parsers.XML11Configuration.parse(XML11Configuration.java:860)
	at java.xml/internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
	at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
	at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
	at java.xml/jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
	at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206)
	at com.code2care.tutorials.MyXmlDomParserExample.main(MyXmlDomParserExample.java:35)

2) java.lang.NullPointerException

There could be many reasons you may get a NullPointerException (operating on a null object!) but if the document.getElementsByTagName(null); gets a null object you get the below,

Exception in thread "main" java.lang.NullPointerException
at java.xml/com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.nextMatchingElementAfter(DeepNodeListImpl.java:197)
at java.xml/com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.item(DeepNodeListImpl.java:146)
at java.xml/com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.getLength(DeepNodeListImpl.java:116)
at com.code2care.tutorials.MyXmlDomParserExample.main(MyXmlDomParserExample.java:39)
---- Element: 1 ----
Exception in thread "main" java.lang.NullPointerException
at com.code2care.tutorials.MyXmlDomParserExample.getElementText(MyXmlDomParserExample.java:54)
at com.code2care.tutorials.MyXmlDomParserExample.main(MyXmlDomParserExample.java:45)

3) org.xml.sax.SAXParseException

If the XML document is not well-formed you get the SAXParseException,

[Fatal Error] countries.xml:14:7: 
The element type "primary_language" must be terminated 
by the matching end-tag "</primary_language>".
Exception in thread "main" org.xml.sax.SAXParseException; 
systemId: file:/C:/eclipse-workspace/countries.xml; 
lineNumber: 14; columnNumber: 7; 
The element type "primary_language" must be terminated by 
the matching end-tag "</primary_language>".

at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:261)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206)
at com.code2care.tutorials.MyXmlDomParserExample.main(MyXmlDomParserExample.java:35)

Reference documents:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap