<s>
SAX	O
(	O
Simple	B-Application
API	I-Application
for	I-Application
XML	I-Application
)	O
is	O
an	O
event-driven	B-Application
online	B-Algorithm
algorithm	I-Algorithm
for	O
lexing	B-Application
and	O
parsing	B-Language
XML	B-Protocol
documents	I-Protocol
,	O
with	O
an	O
API	B-Application
developed	O
by	O
the	O
XML-DEV	O
mailing	O
list	O
.	O
</s>
<s>
SAX	O
provides	O
a	O
mechanism	O
for	O
reading	O
data	O
from	O
an	O
XML	B-Protocol
document	I-Protocol
that	O
is	O
an	O
alternative	O
to	O
that	O
provided	O
by	O
the	O
Document	B-General_Concept
Object	I-General_Concept
Model	I-General_Concept
(	O
DOM	B-General_Concept
)	O
.	O
</s>
<s>
Where	O
the	B-General_Concept
DOM	I-General_Concept
operates	O
on	O
the	O
document	O
as	O
a	O
whole	O
—	O
building	O
the	O
full	O
abstract	B-Data_Structure
syntax	I-Data_Structure
tree	I-Data_Structure
of	O
an	O
XML	B-Protocol
document	I-Protocol
for	O
convenience	O
of	O
the	O
user	O
—	O
SAX	B-Application
parsers	I-Application
operate	O
on	O
each	O
piece	O
of	O
the	O
XML	B-Protocol
document	I-Protocol
sequentially	O
,	O
issuing	O
parsing	B-Language
events	O
while	O
making	O
a	O
single	B-Language
pass	I-Language
through	O
the	O
input	O
stream	O
.	O
</s>
<s>
Unlike	O
DOM	B-General_Concept
,	O
there	O
is	O
no	O
formal	O
specification	O
for	O
SAX	O
.	O
</s>
<s>
The	O
Java	B-Language
implementation	O
of	O
SAX	O
is	O
considered	O
to	O
be	O
normative	O
.	O
</s>
<s>
SAX	O
processes	O
documents	O
state-independently	O
,	O
in	O
contrast	O
to	O
DOM	B-General_Concept
which	O
is	O
used	O
for	O
state-dependent	O
processing	O
of	O
XML	B-Protocol
documents	I-Protocol
.	O
</s>
<s>
A	O
SAX	B-Application
parser	I-Application
only	O
needs	O
to	O
report	O
each	O
parsing	B-Language
event	O
as	O
it	O
happens	O
,	O
and	O
normally	O
discards	O
almost	O
all	O
of	O
that	O
information	O
once	O
reported	O
(	O
it	O
does	O
,	O
however	O
,	O
keep	O
some	O
things	O
,	O
for	O
example	O
a	O
list	O
of	O
all	O
elements	O
that	O
have	O
not	O
been	O
closed	O
yet	O
,	O
in	O
order	O
to	O
catch	O
later	O
errors	O
such	O
as	O
end-tags	O
in	O
the	O
wrong	O
order	O
)	O
.	O
</s>
<s>
Thus	O
,	O
the	O
minimum	O
memory	B-General_Concept
required	O
for	O
a	O
SAX	B-Application
parser	I-Application
is	O
proportional	O
to	O
the	O
maximum	O
depth	O
of	O
the	O
XML	B-Protocol
file	O
(	O
i.e.	O
,	O
of	O
the	O
XML	B-Protocol
tree	O
)	O
and	O
the	O
maximum	O
data	O
involved	O
in	O
a	O
single	O
XML	B-Protocol
event	O
(	O
such	O
as	O
the	O
name	O
and	O
attributes	O
of	O
a	O
single	O
start-tag	O
,	O
or	O
the	O
content	O
of	O
a	O
processing	B-Language
instruction	I-Language
,	O
etc	O
.	O
</s>
<s>
This	O
much	O
memory	B-General_Concept
is	O
usually	O
considered	O
negligible	O
.	O
</s>
<s>
A	O
DOM	B-General_Concept
parser	B-Language
,	O
in	O
contrast	O
,	O
has	O
to	O
build	O
a	O
tree	O
representation	O
of	O
the	O
entire	O
document	O
in	O
memory	B-General_Concept
to	O
begin	O
with	O
,	O
thus	O
using	O
memory	B-General_Concept
that	O
increases	O
with	O
the	O
entire	O
document	O
length	O
.	O
</s>
<s>
This	O
takes	O
considerable	O
time	O
and	O
space	O
for	O
large	O
documents	O
(	O
memory	B-General_Concept
allocation	O
and	O
data-structure	O
construction	O
take	O
time	O
)	O
.	O
</s>
<s>
Because	O
of	O
the	O
event-driven	B-Application
nature	O
of	O
SAX	O
,	O
processing	O
documents	O
is	O
generally	O
far	O
faster	O
than	O
DOM-style	O
parsers	B-Language
,	O
so	O
long	O
as	O
the	O
processing	O
can	O
be	O
done	O
in	O
a	O
start-to-end	O
pass	O
.	O
</s>
<s>
Other	O
tasks	O
,	O
such	O
as	O
sorting	O
,	O
rearranging	O
sections	O
,	O
getting	O
from	O
a	O
link	O
to	O
its	O
target	O
,	O
looking	O
up	O
information	O
on	O
one	O
element	O
to	O
help	O
process	O
a	O
later	O
one	O
and	O
the	O
like	O
require	O
accessing	O
the	O
document	O
structure	O
in	O
complex	O
orders	O
and	O
will	O
be	O
much	O
faster	O
with	O
DOM	B-General_Concept
than	O
with	O
multiple	O
SAX	O
passes	O
.	O
</s>
<s>
Some	O
implementations	O
do	O
not	O
neatly	O
fit	O
either	O
category	O
:	O
a	O
DOM	B-General_Concept
approach	O
can	O
keep	O
its	O
persistent	B-General_Concept
data	I-General_Concept
on	O
disk	O
,	O
cleverly	O
organized	O
for	O
speed	O
(	O
editors	O
such	O
as	O
SoftQuad	O
Author/Editor	O
and	O
large-document	O
browser/indexers	O
such	O
as	O
DynaText	O
do	O
this	O
)	O
;	O
while	O
a	O
SAX	O
approach	O
can	O
cleverly	O
cache	O
information	O
for	O
later	O
use	O
(	O
any	O
validating	O
SAX	B-Application
parser	I-Application
keeps	O
more	O
information	O
than	O
described	O
above	O
)	O
.	O
</s>
<s>
Due	O
to	O
the	O
nature	O
of	O
DOM	B-General_Concept
,	O
streamed	O
reading	O
from	O
disk	O
requires	O
techniques	O
such	O
as	O
lazy	O
evaluation	O
,	O
caches	O
,	O
virtual	B-Architecture
memory	I-Architecture
,	O
persistent	B-General_Concept
data	I-General_Concept
structures	O
,	O
or	O
other	O
techniques	O
(	O
one	O
such	O
technique	O
is	O
disclosed	O
in	O
US	O
patent	O
5557722	O
)	O
.	O
</s>
<s>
Processing	O
XML	B-Protocol
documents	I-Protocol
larger	O
than	O
main	O
memory	B-General_Concept
is	O
sometimes	O
thought	O
impossible	O
because	O
some	O
DOM	B-General_Concept
parsers	B-Language
do	O
not	O
allow	O
it	O
.	O
</s>
<s>
However	O
,	O
it	O
is	O
no	O
less	O
possible	O
than	O
sorting	O
a	O
dataset	O
larger	O
than	O
main	O
memory	B-General_Concept
using	O
disk	O
space	O
as	O
memory	B-General_Concept
to	O
sidestep	O
this	O
limitation	O
.	O
</s>
<s>
The	O
event-driven	B-Application
model	O
of	O
SAX	O
is	O
useful	O
for	O
XML	B-Protocol
parsing	B-Language
,	O
but	O
it	O
does	O
have	O
certain	O
drawbacks	O
.	O
</s>
<s>
Virtually	O
any	O
kind	O
of	O
XML	B-Language
validation	I-Language
requires	O
access	O
to	O
the	O
document	O
in	O
full	O
.	O
</s>
<s>
The	O
most	O
trivial	O
example	O
is	O
that	O
an	O
attribute	O
declared	O
in	O
the	O
DTD	B-Language
to	O
be	O
of	O
type	O
IDREF	O
,	O
requires	O
that	O
there	O
be	O
only	O
one	O
element	O
in	O
the	O
document	O
that	O
uses	O
the	O
same	O
value	O
for	O
an	O
ID	O
attribute	O
.	O
</s>
<s>
To	O
validate	O
this	O
in	O
a	O
SAX	B-Application
parser	I-Application
,	O
one	O
must	O
keep	O
track	O
of	O
all	O
ID	O
attributes	O
(	O
any	O
one	O
of	O
them	O
might	O
end	O
up	O
being	O
referenced	O
by	O
an	O
IDREF	O
attribute	O
at	O
the	O
very	O
end	O
)	O
;	O
as	O
well	O
as	O
every	O
IDREF	O
attribute	O
until	O
it	O
is	O
resolved	O
.	O
</s>
<s>
Additionally	O
,	O
some	O
kinds	O
of	O
XML	B-Protocol
processing	O
simply	O
require	O
having	O
access	O
to	O
the	O
entire	O
document	O
.	O
</s>
<s>
XSLT	B-Application
and	O
XPath	B-Language
,	O
for	O
example	O
,	O
need	O
to	O
be	O
able	O
to	O
access	O
any	O
node	O
at	O
any	O
time	O
in	O
the	O
parsed	B-Language
XML	B-Protocol
tree	O
.	O
</s>
<s>
While	O
a	O
SAX	B-Application
parser	I-Application
may	O
well	O
be	O
used	O
to	O
construct	O
such	O
a	O
tree	O
initially	O
,	O
SAX	O
provides	O
no	O
help	O
for	O
such	O
processing	O
as	O
a	O
whole	O
.	O
</s>
<s>
A	O
parser	B-Language
that	O
implements	O
SAX	O
(	O
i.e.	O
,	O
a	O
SAX	B-Application
Parser	I-Application
)	O
functions	O
as	O
a	O
stream	O
parser	B-Language
,	O
with	O
an	O
event-driven	B-Application
API	B-Application
.	O
</s>
<s>
The	O
user	O
defines	O
a	O
number	O
of	O
callback	O
methods	O
that	O
will	O
be	O
called	O
when	O
events	O
occur	O
during	O
parsing	B-Language
.	O
</s>
<s>
Some	O
events	O
correspond	O
to	O
XML	B-Protocol
objects	O
that	O
are	O
easily	O
returned	O
all	O
at	O
once	O
,	O
such	O
as	O
comments	O
.	O
</s>
<s>
However	O
,	O
XML	B-Protocol
elements	O
can	O
contain	O
many	O
other	O
XML	B-Protocol
objects	O
,	O
and	O
so	O
SAX	O
represents	O
them	O
as	O
does	O
XML	B-Protocol
itself	O
:	O
by	O
one	O
event	O
at	O
the	O
beginning	O
,	O
and	O
another	O
at	O
the	O
end	O
.	O
</s>
<s>
SAX	O
parsing	B-Language
is	O
unidirectional	O
;	O
previously	O
parsed	B-Language
data	O
cannot	O
be	O
re-read	O
without	O
starting	O
the	O
parsing	B-Language
operation	O
again	O
.	O
</s>
<s>
For	O
example	O
,	O
XML	B-Protocol
attributes	O
are	O
typically	O
provided	O
as	O
name	O
and	O
value	O
arguments	O
passed	O
to	O
element	O
events	O
,	O
but	O
can	O
also	O
be	O
provided	O
as	O
separate	O
events	O
,	O
or	O
via	O
a	O
hash	O
table	O
or	O
similar	O
collection	O
of	O
all	O
the	O
attributes	O
.	O
</s>
<s>
For	O
another	O
,	O
some	O
implementations	O
provide	O
"	O
Init	O
"	O
and	O
"	O
Fin	O
"	O
callbacks	O
for	O
the	O
very	O
start	O
and	O
end	O
of	O
parsing	B-Language
;	O
others	O
do	O
n't	O
.	O
</s>
<s>
Given	O
the	O
following	O
XML	B-Protocol
document	I-Protocol
:	O
</s>
<s>
This	O
XML	B-Protocol
document	I-Protocol
,	O
when	O
passed	O
through	O
a	O
SAX	B-Application
parser	I-Application
,	O
will	O
generate	O
a	O
sequence	O
of	O
events	O
like	O
the	O
following	O
:	O
</s>
<s>
XML	B-Protocol
Text	O
node	O
,	O
with	O
data	O
equal	O
to	O
"	O
Post-text.	O
"	O
</s>
<s>
Note	O
that	O
the	O
first	O
line	O
of	O
the	O
sample	O
above	O
is	O
the	O
XML	B-Protocol
Declaration	O
and	O
not	O
a	O
processing	B-Language
instruction	I-Language
;	O
as	O
such	O
it	O
will	O
not	O
be	O
reported	O
as	O
a	O
processing	B-Language
instruction	I-Language
event	O
(	O
although	O
some	O
SAX	O
implementations	O
provide	O
a	O
separate	O
event	O
just	O
for	O
the	O
XML	B-Protocol
declaration	O
)	O
.	O
</s>
<s>
Many	O
parsers	B-Language
,	O
for	O
example	O
,	O
return	O
separate	O
text	O
events	O
for	O
numeric	O
character	O
references	O
.	O
</s>
<s>
Thus	O
in	O
the	O
example	O
above	O
,	O
a	O
SAX	B-Application
parser	I-Application
may	O
generate	O
a	O
different	O
series	O
of	O
events	O
,	O
part	O
of	O
which	O
might	O
include	O
:	O
</s>
