By jj83777
via tutorials.jenkov.com
Published: Nov 23 2009 / 11:44
This text compares Java's two stream oriented XML parsers, SAX and StAX, and explains the difference in a "push" model and "pull" model. It also lists other differences in these two API's.
If you have ever wondered which of these two API's to use, this text may help you decide.



Comments
yakkoh replied ago:
Questions:
why event, why not token or doohickey or thing?
why is it so complicated?
why no open and close methods? XML is not a stream?
Jakob Jenkov replied ago:
I don't know :-) I didn't write the API's. But you are right, sometimes XML processing seems a bit more complicated than it should be.
anon2 replied ago:
XML is actually not ideally suited for stream processing, because without loading the entire document in memory, a lot of context information is lost
VTD-XML is better suited for processing XML than DOM and SAX
jeffb replied ago:
Most of the time I don't use sax, or stax because they're more complex to use than a few open source libraries I've found.
I use xstream to map objects to xml.
http://xstream.codehaus.org
When I am processing xml and I can't bind it my preferred library is XOM.
http://www.xom.nu/
Both xstream, and xom can process xml in parts so all the xml doesn't need to be in ram at once.
There is also jdom which is another tree based xml api that you may want to check out (personally I prefer xom but you may like jdom more).
http://www.jdom.org/
anon2 replied ago:
xom and jdom all suffer from excessive memory usages due to object allocations
Jakob Jenkov replied ago:
Thanks a lot for all of these interesting comments. It is always inspiring to see new ways of doing "the same". There are so many XML frameworks (and Java frameworks in general) that it can be really hard to go through all of them one by one.
Jakob Jenkov replied ago:
By the way, I prefer DOM4J over JDOM any time. Better performance, and it seems to be more flexible too.
anon2 replied ago:
DOM4J is not match for vtd-xml, (10x faster in a typical use case)
Jakob Jenkov replied ago:
Interesting - I'll have to check out that vtd-xml :-) ... sounds promising!
unkind replied ago:
i totally agree with anon2 vtd-xml is very fast (5 x to 12x faster than jdom)
Voters For This Link (12)
Voters Against This Link (0)