Difference between DOM and SAX Parsers in Java

来源:互联网 发布:淘宝注册网店步骤费用 编辑:程序博客网 时间:2024/04/29 15:21
Difference between SAX and DOM Parser is very popularJavainterview and often asked when interviewed on Java and XML. Both DOM and SAX parser are extensively used toread and parse XML file in java and have there own set of advantage and disadvantage which we will cover in thisarticle. Though there is another way ofreading xml file usingxpath in Java which is more selective approach like SQLstatements people tend to stick with XML parsers. DOM Parser vs SAX parsers are also often viewed in terms of speed, memory consumption and there ability to process large xml files.

Difference between DOM and SAX XML Parser in Java

DOM XML Parser in Java

difference between DOM and XML parsers in Java
DOM Stands forDocument Object Model and it represent an XML Document into tree format which each element representing tree branches.DOM Parser creates an In Memory tree representation of XML file and then parses it, so it requires more memory and its advisable to haveincreased heap size for DOM parser in order to avoid Java.lang.OutOfMemoryError:java heap space . Parsing XML file using DOM parser is quite fast if XML file is small but if you try to read a large XML file using DOM parser there is morechances that it will take a long time or even may not be able to load it completely simply because it requires lot of memory to create XML Dom Tree. Java provides support DOM Parsing and you can parse XML files in Java using DOM parser. DOM classes are in w3c.dom package whileDOM Parser for Java is in JAXP (Java API for XML Parsing) package.

SAX XML Parser in Java

SAX Stands for Simple API for XML Parsing. This is an event based XML Parsing and itparse XML filestep by step so much suitable for large XML Files. SAX XML Parser fires event when it encountered opening tag, element or attribute and the parsing works accordingly. It’s recommended to use SAX XML parser for parsing large xml files in Java because it doesn't require to load whole XML file in Java and it can read a big XML file in small parts. Java provides support for SAX parser and you can parse any xml file in Java using SAX Parser, I have covered example of reading xml file using SAX Parser here. One disadvantage of using SAX Parser in java is thatreading XML file in Java using SAX Parser requires more code in comparison of DOM Parser.


Difference between DOM and SAX XML Parser

Here are few high level differences between DOM parser and SAX Parserin Java:

1) DOM parser loads whole xml document in memory while SAX only loads small part of XML file in memory.

2) DOM parser is faster than SAX because it access whole XML document in memory.

3) SAX parser in Java is better suitable for large XML file than DOM Parser because it doesn't require much memory.

4) DOM parser works on Document Object Model while SAX is an event based xml parser.


That’s all on difference between SAX and DOM parsers in Java, now it’s up to you on which XML parser you going to choose. I recommend use DOM parser over SAX parser if XML file is small enough and go with SAX parser if you don’t know size of xml files to be processed or they are large.


Read more: http://javarevisited.blogspot.com/2011/12/difference-between-dom-and-sax-parsers.html#ixzz2ki8Plft7
原创粉丝点击