You are on page 1of 6

XML SAX

XML Parser
• DOM đọc nguyên một XML file rồi parse nó
thành một Tree có phân cấp trong bộ nhớ,
tức là một node cha của Document có những
nodes con đại diện cho comments, elements,
attribute và text
• Trong khi đó SAX đọc một XML file và trong
khi parse sẽ generate những Events cho hay
khi nào nó gặp phải những XML thành phần.
SAX không tạo ra một Tree nào cả, nên các
ứng dụng tùy thuộc vào cách ta handle các
Events từ SAX
NodeTypes - Named Constants
NodeType Named Constant

1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE
SAX vs. DOM
• The DOM specification defines a tree-based
approach to navigating an XML document. In
other words, a DOM parser processes XML data
and creates an object-oriented hierarchical
representation of the document that you can
navigate at run-time.
• The SAX specification defines an event-based
approach whereby parsers scan through XML
data, calling handler functions whenever certain
parts of the document (e.g., text nodes or
processing instructions) are found.
SAX vs. DOM
• SAX is a streaming interface — applications receive
information from XML documents in a continuous
stream, with no backtracking or navigation allowed.
This approach makes SAX extremely efficient, handing
XML documents of nearly any size in linear time and
near-constant memory, but it also places greater
demands on the software developer's skills.
• Tree-based interfaces, like the Document Object Model
(DOM), make exactly the opposite trade-off: they are
much simpler for developers, but at the cost of
significant time and computer resources.
Events vs. Trees
• There are two major types of XML (or SGML) APIs:
• Tree-based APIs These map an XML document into an internal tree
structure, then allow an application to navigate that tree. The
Document Object Model (DOM) working group at the World-Wide
Web Consortium (W3C) maintains a recommended tree-based API
for XML and HTML documents, and there are many such APIs from
other sources.
• Event-based APIs An event-based API, on the other hand, reports
parsing events (such as the start and end of elements) directly to the
application through callbacks, and does not usually build an internal
tree. The application implements handlers to deal with the different
events, much like handling events in a graphical user interface. SAX is
the best known example of such an API.

You might also like