How to parse XML in Java


In this tutorial, we will read the XML file via DOM XML parser using Java program. DOM parser loads the whole XML document into memory.

What is DOM?

The Document Object Model (DOM) is a cross-platform and language-independent application programming interface that treats an HTML, XHTML, or XML document as a tree structure wherein each node is an object representing a part of the document. The objects can be manipulated programmatically and any visible changes occurring, as a result, may then be reflected in the display of the document. For more info read the Wiki

student.xml

<?xml version="1.0"?>
<college>
    <student id="101">
        <name>WebSparrow</name>
        <rollnumber>2016</rollnumber>
        <course>Ph.D</course>
        <nationality>India</nationality>
        <mobile>+91XXXXXXXXXX</mobile>
    </student>
    <student id="102">
        <name>Smith</name>
        <rollnumber>1234</rollnumber>
        <course>B.Sc</course>
        <nationality>USA</nationality>
        <mobile>+1XXXXXXXXXX</mobile>
    </student>    
</college>

XMLFileParser.java

package org.websparrow;

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

public class XMLFileParser {

    public static void main(String[] args) {
        try {
            File readXml = new File("F:/student.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document d = db.parse(readXml);
            d.getDocumentElement().normalize();
            System.out.println("Root Element : " + d.getDocumentElement().getNodeName());
            System.out.println("======================");
            NodeList nodeList = d.getElementsByTagName("student");
            for (int index = 0; index < nodeList.getLength(); index++) {
                Node node = nodeList.item(index);
                System.out.println("\nCurrent Element : " + node.getNodeName());
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element e = (Element) node;
                    System.out.println("Student id: " + e.getAttribute("id"));
                    System.out.println("Name : " + e.getElementsByTagName("name").item(0).getTextContent());
                    System.out.println("Roll Number : " + e.getElementsByTagName("rollnumber").item(0).getTextContent());
                    System.out.println("Course : " + e.getElementsByTagName("course").item(0).getTextContent());
                    System.out.println("Nationality : " + e.getElementsByTagName("nationality").item(0).getTextContent());
                    System.out.println("Mobile : " + e.getElementsByTagName("mobile").item(0).getTextContent());

                }

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

}

Output:

Root Element : college
======================

Current Element : student
Student id: 101
Name : WebSparrow
Roll Number : 2016
Course : Ph.D
Nationality : India
Mobile : +91XXXXXXXXXX

Current Element : student
Student id: 102
Name : Smith
Roll Number : 1234
Course : B.Sc
Nationality : USA
Mobile : +1XXXXXXXXXXX

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.