Skip to content
XML

DOM and SAX Parsing

Two models for reading XML.

#dom#sax#parser

Code

xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<!-- DOM: loads the whole tree into memory; random access, editable -->
<!-- SAX: event-driven streaming; low memory, forward-only, read-only -->
<catalog parser="dom-or-sax">
  <item id="1">DOM builds a node tree you can query with XPath</item>
  <item id="2">SAX fires startElement/endElement callbacks per node</item>
  <item id="3">Use DOM for small docs, SAX for huge streams</item>
</catalog>