xml sax
TRANSCRIPT
-
8/7/2019 XML SAX
1/6
XML SAX
-
8/7/2019 XML SAX
2/6
XML Parser
DOM c nguyn mt XML file ri parse nthnh mt Tree c phn cp trong b nh, tcl mt node cha ca Document c nhng
nodes con i din cho comments,elements
,attributev text Trong khi SAX c mt XML file v trong
khi parse s generate nhng Events cho haykhi no n gp phi nhng XML thnh phn.
SAX khng to ra mt Tree no c, nn ccng dng ty thuc vo cch ta handle ccEvents t SAX
-
8/7/2019 XML SAX
3/6
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
-
8/7/2019 XML SAX
4/6
SAX vs. DOM
TheDOM specificationdefinesatree-basedapproachtonavigatingan XML document.Inother words,aDOM parserprocesses XML dataandcreatesanobject-orientedhierarchicalrepresentationofthedocumentthatyoucannavigateatrun-time.
The SAX specificationdefinesanevent-basedapproach wherebyparsersscanthrough XML
data,callinghandlerfunctions whenevercertainpartsofthedocument (e.g.,textnodesorprocessinginstructions)arefound.
-
8/7/2019 XML SAX
5/6
SAX vs. DOM
SAX isastreaming interface applicationsreceiveinformationfrom XML documentsinacontinuousstream, withnobacktrackingornavigationallowed.
Thisappr
o
achmakes
SAXextremely
efficient,
han
d
ing
XML documentsofnearlyanysizeinlineartimeandnear-constantmemory,butitalsoplacesgreaterdemandsonthesoftwaredeveloper'sskills.
Tree-based interfaces,liketheDocumentObject Model(DOM),makeexactlytheoppositetrade-off:theyaremuchsimplerfordevelopers,butatthecostofsignificanttimeandcomputerresources.
-
8/7/2019 XML SAX
6/6
Events vs. Trees
TherearetwomajortypesofXML (or SGML) APIs:
Tree-based APIs Thesemapan XML documentintoaninternaltreestructure,thenallow anapplicationtonavigatethattree.
TheDocumentObject Model (DOM) workinggroupatthe
World-Wide Web Consortium (W3C)maintainsarecommendedtree-based APIfor XML and HTML documents,
andtherearemanysuch APIsfromothersources.
Event-based APIs Anevent-basedAPI,ontheotherhand,
repo
rtsparsingevents (suchasthestartand
end
o
felements)directlytotheapplicationthroughcallbacks,anddoesnot
usuallybuildaninternaltree.Theapplicationimplements
handlerstodeal withthedifferentevents,muchlikehandling
eventsinagraphicaluserinterface. SAX isthebestknown
exampleofsuchan API.