Signup/Sign In
LAST UPDATED: AUGUST 5, 2021

How to read XML file in Java

In this post, we are going to learn to read an XML file using the Java code. The XML is an Extensible Markup Language document file that is used to store and transport data.

Java provides an XML parser library to read the XML document. So, we must import these libraries before writing code to read the file.

Here, we are using a sample XML file students.xml that contains some data and we will read that using the Java code.

// students.xml

<students>
    <student id="101">
        <Name>John</Name>
        <id>11001</id>
        <location>India</location>
    </student>
    <student id="102">
        <Name>Alex</Name>
        <id>11002</id>
        <location>Russia</location>
    </student>
    <student id="103">
        <Name>Rohan</Name>
        <id>11003</id>
        <location>USA</location>
    </student>
</students>

Time for an Example:

Let's create an example to read an XML file. Here, first we created an instance of doucment and then parse it to read the file. The getDocumentElement() method is used to read the root element and then by using the loop we iterate all the tags of the document.

package myjavaproject;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException{  
		DocumentBuilderFactory dBfactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dBfactory.newDocumentBuilder();
		// Fetch XML File
		Document document = builder.parse(new File("students.xml"));
		document.getDocumentElement().normalize();
		//Get root node
		Element root = document.getDocumentElement();
		System.out.println(root.getNodeName());
		//Get all students
		NodeList nList = document.getElementsByTagName("student");
		System.out.println(".................................");

		for (int i = 0; i < nList.getLength(); i++)
		{
			Node node = nList.item(i);
			System.out.println();    //Just a separator
			if (node.getNodeType() == Node.ELEMENT_NODE)
			{
				//Print each student's detail
				Element element = (Element) node;
				System.out.println("Student id : "    + element.getAttribute("id"));
				System.out.println("Name : "  + element.getElementsByTagName("Name").item(0).getTextContent());
				System.out.println("Roll No : "   + element.getElementsByTagName("id").item(0).getTextContent());
				System.out.println("Location : "    + element.getElementsByTagName("location").item(0).getTextContent());
			}
		}
	}
}


students
.................................
Student id : 101
Name : John
Roll No : 11001
Location : India

Student id : 102
Name : Alex
Roll No : 11002
Location : Russia

Student id : 103
Name : Rohan
Roll No : 11003
Location : USA

Live Example:

You can test this Java code using the Online Java Compiler. Here, we have attached an XML file to read the data.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.