Source: https://www.javatpoint.com/how-to-read-xml-file-in-javaHow to Read XML File in Java Reading XML file in Java is much different from reading other files like .docx and .txt because XML file contains data between the tags. Java provides many ways to parse an XML file. There are two parsers in Java which parses an XML file: Java DOM Parser Java SAX Parser Java DOM Parser The DOM API provides the classes to read and write an XML file. We can create, delete, modify, and rearrange the node using the DOM API. DOM parser parses the entire XML file and creates a DOM object in the memory. It models an XML file in a tree structure for easy traversal and manipulation. In DOM everything in an XML file is a node. The node represents a component of an XML file. DOM parser is slow in process and occupies lots of memory when it loads an XML file into memory. We must have followed the process to read an XML file in Java: Instantiate XML file: DOM parser loads the XML file into memory and consider every tag as an element. Get root node: Document class provides the getDocumentElement() method to get the root node and the element of the XML file. Get all nodes: The getElementByTagName() method retrieves all the specific tag name from the XML file. Where ELEMENT_NODE type refers to a non-text node which has sub-elements. If we need to access all nodes from the starting, including the root node, we can recursively call the getChildElement() method. Get Node by text value: We can use getElementByTextValue() method in order to search for a node by its value. Get Node by attribute value: If we want to search a node by the value of a specific attribute, we can use the getElementByTagName() method along with getAttribute() method. Steps to Read XML File in Java Using eclipse Step 1: Create a simple Java project. Step 2: Create a class file and provide a class file name. We have created the class file with the name ReadXMLFileExample1. Step 3: Write the following code. Step 4: Download dom-2.3.0-jaxb-1.0.6.jar file: Click here... Step 5: Create a lib folder in the project. Step 6: Copy dom-2.3.0-jaxb-1.0.6.jar file and paste into the lib folder. Step 7: Set the class path: Right-click on the project->Build Path->Configure Build Path->Add External JARs->Select the JAR file->click on Open button->Apply and Close. Step 8: Create an XML file. We have created an XML file with name XMLFile.xml and write the following data into it. Step 9: Run the project. Creating XML file: XMLFile.xml 101 Naman Kumar Math 83 102 Kapil Kumar Chemistry 60 103 Harsh Singh English 70 104 Jitesh Singh Physics 76 Example of reading XML file using DOM Parser 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 ReadXMLFileExample1 { public static void main(String argv[]) { try { //creating a constructor of file class and parsing an XML file File file = new File("F:\\XMLFile.xml"); //an instance of factory that gives a document builder DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //an instance of builder to parse the specified xml file DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element: " + doc.getDocumentElement().getNodeName()); NodeList nodeList = doc.getElementsByTagName("student"); // nodeList is not iterable, so we are using for loop 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("Student id: "+ eElement.getElementsByTagName("id").item(0).getTextContent()); 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("Subject: "+ eElement.getElementsByTagName("subject").item(0).getTextContent()); System.out.println("Marks: "+ eElement.getElementsByTagName("marks").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } Output: Root element: class Node Name: student Student id: 101 First Name: Naman Last Name: Kumar Subject: Math Marks: 83 Node Name: student Student id: 102 First Name: Kapil Last Name: Kumar Subject: Chemistry Marks: 60 Node Name: student Student id: 103 First Name: Harsh Last Name: Singh Subject: English Marks: 70 Node Name: student Student id: 104 First Name: Jitesh Last Name: Singh Subject: Physics Marks: 76