XML Parsing

All Java Topics
Last updated: May 25, 2026
Author: ManaCoding Team

XML parsing in Java is used to read, modify, and process XML files. Java provides multiple APIs like DOM, SAX, and StAX for handling XML data.

📝 Syntax
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
💻 Example Program
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import java.io.File;

public class Main {

  public static void main(String[] args) {

    try {

      File file = new File("students.xml");

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();

      Document doc = builder.parse(file);
      doc.getDocumentElement().normalize();

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

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

        Node node = nodeList.item(i);

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

          Element element = (Element) node;

          System.out.println("ID: " + element.getElementsByTagName("id").item(0).getTextContent());
          System.out.println("Name: " + element.getElementsByTagName("name").item(0).getTextContent());
          System.out.println("-------------------");

        }

      }

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

  }
}

// Sample XML (students.xml):
/*
<students>
  <student>
    <id>1</id>
    <name>John</name>
  </student>
  <student>
    <id>2</id>
    <name>Alex</name>
  </student>
</students>
*/

// Output:
// ID: 1
// Name: John
// -------------------
// ID: 2
// Name: Alex
💡 What is XML?
  • 1 Extensible Markup Language.
  • 2 Used for structured data.
  • 3 Platform independent.
  • 4 Self-descriptive format.
💡 Types of XML Parsers
  • 1 DOM Parser – loads full document in memory.
  • 2 SAX Parser – event-driven, memory efficient.
  • 3 StAX Parser – streaming API.
💡 DOM Parser Features
  • 1 Loads entire XML into memory.
  • 2 Easy to navigate.
  • 3 Good for small files.
  • 4 Supports read and modify.
💡 Why XML Parsing?
  • 1 Used in configuration files.
  • 2 Used in data exchange.
  • 3 Used in legacy systems.
  • 4 Supports structured data handling.
Quick Summary
  • XML parsing is used to read XML files in Java.
  • DOM and SAX are commonly used parsers.
  • DOM is memory heavy, SAX is lightweight.
  • Used in legacy and enterprise systems.
FAQs
What is XML parsing?
It is the process of reading and processing XML files.
What are types of XML parsers?
DOM, SAX, and StAX parsers.
Which parser is memory efficient?
SAX parser.
When to use DOM parser?
For small XML files.
What is XML used for?
Data storage and exchange.
🎯 Interview Questions
Q1. What is XML parsing?
Answer: It is the process of reading and processing XML files.
Q2. What are types of XML parsers?
Answer: DOM, SAX, and StAX parsers.
Q3. Which parser is memory efficient?
Answer: SAX parser.
Q4. When to use DOM parser?
Answer: For small XML files.
Q5. What is XML used for?
Answer: Data storage and exchange.
Quiz

Which XML parser loads the entire document into memory?