Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

XML Parsing: DOM and SAX

Lesson 29 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

XML Parsing: DOM and SAX

What is XML Parsing?

XML parsing is the process of reading an XML document and making its data available to a program. Two primary parsing models:

  1. DOM (Document Object Model) – load entire document into memory as a tree
  2. SAX (Simple API for XML) – event-driven, processes XML sequentially

DOM Parsing

DOM parsing loads the entire XML into memory as a tree structure.

DOM Parsing in JavaScript (Browser)

const xmlString = `<?xml version="1.0"?>
<students>
  <student id="1">
    <name>Alice</name>
    <marks>92</marks>
  </student>
  <student id="2">
    <name>Bob</name>
    <marks>85</marks>
  </student>
</students>`;

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");

// Access elements
const students = xmlDoc.getElementsByTagName("student");
for (let i = 0; i < students.length; i++) {
    const name = students[i].getElementsByTagName("name")[0].textContent;
    const marks = students[i].getElementsByTagName("marks")[0].textContent;
    const id = students[i].getAttribute("id");
    console.log(`ID: ${id}, Name: ${name}, Marks: ${marks}`);
}

DOM Parsing in Java

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("students.xml"));
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(element.getElementsByTagName("name")
                                  .item(0).getTextContent());
    }
}

SAX Parsing

SAX is event-driven and does not load the entire document into memory.

// SAX Handler in Java
public class StudentHandler extends DefaultHandler {
    boolean inName = false;

    @Override
    public void startElement(String uri, String localName,
                             String qName, Attributes attributes) {
        if (qName.equalsIgnoreCase("name")) {
            inName = true;
        }
        if (qName.equalsIgnoreCase("student")) {
            System.out.println("ID: " + attributes.getValue("id"));
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        if (inName) {
            System.out.println("Name: " + new String(ch, start, length));
            inName = false;
        }
    }
}

DOM vs SAX Comparison

FeatureDOMSAX
MemoryHigh (entire tree)Low (sequential)
SpeedSlower for large filesFaster
NavigationRandom accessSequential only
ModificationCan modify treeRead-only
Use caseSmall/medium XMLLarge XML files
ComplexitySimpler APIMore complex

When to Use Which

  • Use DOM when you need to navigate, search, or modify XML freely
  • Use SAX when processing very large XML files where memory is a concern
Key Takeaway: DOM parsing creates a full in-memory tree (easy to navigate), while SAX parsing is event-driven and memory-efficient. Choose based on file size and whether you need random access to the XML structure.