Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Read XML File in Java using DOM parser example

In this tutorial we are going to see how to use the built in Java DOM parser in order to parse an XML file into a DOM Object As you might know, the DOM object has the classic Tree Structure with nodes, elements, attributes etc… Remember that DOM parser reads the XML file and constructs the DOM object in the memory. Then, you have to traverse the DOM like you would do a tree structure, node by node to get the element you want. Of course there are some more advanced parsers (like Jsoup for instance, witch is more for HTML Parsing) that offer more flexible APIs.

As we said, the DOM parser creates the DOM object in its whole inside the memory. So as you can imagine this might be memory consuming for large files. You can always use a SAX parser to get around this problem.

Here we have a simple XML File:

testFile.xml

<?xml version="1.0"?>
<company>

	<employee id="1">
		<firstname>James</firstname>
		<lastname>Harley</lastname>
        <email>james@example.org</email>
		<department>Human Resources</department>
		<salary>1000</salary>
	</employee>

	<employee id="2">
		<firstname>John</firstname>
		<lastname>May</lastname>
		<email>john@example.org</email>
		<department>Logistics</department>
		<salary>400</salary>
	</employee>

</company>

ReadXMLFileUsingDom.java

package com.javacodegeeks.java.core;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFileUsingDom {

	public static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\files\\testFile.xml";

	public static void main(String argv[]) {

		try {

			File xmlFile = new File(xmlFilePath);

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

			Document doc = documentBuilder.parse(xmlFile);

			doc.getDocumentElement().normalize();

			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

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

			System.out.println("===============================================================");

			//do this the old way, because nodeList is not iterable
			for (int itr = 0; itr < nodeList.getLength(); itr++) {

				Node node = nodeList.item(itr);

				System.out.println("\nNode Name :" + node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) node;

					System.out.println("Employee id : "
							+ eElement.getAttribute("id"));
					System.out.println("First Name : "
							+ eElement.getElementsByTagName("firstname")
									.item(0).getTextContent());
					System.out.println("Last Name : "
							+ eElement.getElementsByTagName("lastname").item(0)
									.getTextContent());
					System.out.println("Email : "
							+ eElement.getElementsByTagName("email").item(0)
									.getTextContent());
					System.out.println("Department : "
							+ eElement.getElementsByTagName("department").item(0)
									.getTextContent());
					System.out.println("Salary : "
							+ eElement.getElementsByTagName("salary").item(0)
									.getTextContent());

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Output:

Root element :company
===============================================================

Node Name :employee
Employee id : 1
First Name : James
Last Name : Harley
Email : james@example.org
Department : Human Resources
Salary : 1000

Node Name :employee
Employee id : 2
First Name : John
Last Name : May
Email : john@example.org
Department : Logistics
Salary : 400

Now let’s see how to read and parse the XML file to a DOM object and loop the nodes one by one printing element and attribute names and values.

package com.javacodegeeks.java.core;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFileUsingDom {
	
	public static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\files\\testFile.xml";

	public static void main(String[] args) {

		try {

			File xmlFile = new File(xmlFilePath);

			DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

			Document document = documentBuilder.parse(xmlFile);

			System.out.println("Root element :"+ document.getDocumentElement().getNodeName());
			System.out.println("===============================");

			

			if (document.hasChildNodes()) {

				printNodeList(document.getChildNodes());

			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}

	private static void printNodeList(NodeList nodeList) {

		for (int count = 0; count < nodeList.getLength(); count++) {

			Node elemNode = nodeList.item(count);

			if (elemNode.getNodeType() == Node.ELEMENT_NODE) {

				// get node name and value
				System.out.println("\nNode Name =" + elemNode.getNodeName()
						+ " [OPEN]");
				System.out.println("Node Content =" + elemNode.getTextContent());

				if (elemNode.hasAttributes()) {

					NamedNodeMap nodeMap = elemNode.getAttributes();

					for (int i = 0; i < nodeMap.getLength(); i++) {

						Node node = nodeMap.item(i);
						System.out.println("attr name : " + node.getNodeName());
						System.out.println("attr value : "
								+ node.getNodeValue());

					}

				}

				if (elemNode.hasChildNodes()) {
                    //recursive call if the node has child nodes
					printNodeList(elemNode.getChildNodes());

				}

				System.out.println("Node Name =" + elemNode.getNodeName()
						+ " [CLOSE]");

			}

		}

	}

}

Output:

Root element :company
===============================

Node Name =company
Node Content =

	
		James
		Harley
        james@example.org
		Human Resources
		1000
	

	
		John
		May
		john@example.org
		Logistics
		400
	



Node Name =employee
Node Content =
		James
		Harley
        james@example.org
		Human Resources
		1000
	
attr name : id
attr value : 1

Node Name =firstname
Node Content =James

Node Name =lastname
Node Content =Harley

Node Name =email
Node Content =james@example.org

Node Name =department
Node Content =Human Resources

Node Name =salary
Node Content =1000

Node Name =employee
Node Content =
		John
		May
		john@example.org
		Logistics
		400
	
attr name : id
attr value : 2

Node Name =firstname
Node Content =John

Node Name =lastname
Node Content =May

Node Name =email
Node Content =john@example.org

Node Name =department
Node Content =Logistics

Node Name =salary
Node Content =400

 
This was an example on how to read XML File in Java using DOM parser.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books