Difference between DOM and SAX Parsers in Java
2013-11-16 13:39
330 查看
Difference between SAX and DOM Parser is very popularJava
interview 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 SQL
statements 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.
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.
step 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.
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
interview 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 SQL
statements 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
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
相关文章推荐
- Difference between ArrayList and LinkedList in Java
- Difference between Comparator and Comparable in Java
- Difference between ArrayList and Vector in Java
- Java - Difference between Wait and Sleep , Yield in Java
- Difference between Association, Composition and Aggregation in Java, UML and Object Oriented Program
- Difference between <? super T> and <? extends T> in Java
- Difference between Stack and Heap memory in Java
- Difference between Externalizable and Serializable in Java
- Difference between Stack and Heap in Java
- Difference between start and run method in Thread – Java Tutorial
- Java - Difference between ArrayList and Vector in Java
- difference between Interface and abstract class in Java
- What's the difference between Future and FutureTask in Java?
- Difference between Vector and ArrayList in java?
- Difference between List and Set in Java Collection
- the difference between final and static in java
- Difference between HashSet and HashMap in Java
- Difference between Serializable and Externalizable in Java Serialization
- Difference between getmessage and getlocalizedmessage in exceptions Java?
- 26.Difference between protocol in objective c and interfaces in java?