XML interview question · Most asked
Compare DOM, SAX and StAX (or pull parsing).
Summary
DOM loads the whole document into a tree: easy random access and XPath, but memory grows with the document (often 5-10x the file). SAX pushes events (start element, text, end element) to callbacks: constant memory, forward-only, more state to manage. StAX/pull parsers let your code ask for the next event: constant memory with simpler control flow.
Longer Answer
Use DOM for configs and messages, streaming for large exports and feeds. Python's iterparse and PHP's XMLReader combine streaming with small subtrees.
Key Points
- DOM: whole tree.
- SAX: push events.
- StAX: pull events.
- Choose by size and access pattern.
Deep dive: XML Tutorial: DOM vs Streaming Parsers
Explanation
What interviewers look for
Memory trade-off and practical choice.
More most-asked XML questions
- Explain XML namespaces and the default namespace.
- Your XPath query returns nothing, but the elements are clearly there. What's the likely cause?
- What's the difference between well-formed and valid XML?
- What is an XXE attack and how do you prevent it?
- How does XSLT work? Explain templates and apply-templates.
- How would you process a 10 GB XML file?
- What is XML and where is it still used today?
- Which characters must be escaped in XML, and what is CDATA?