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
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 DOM and SAX Parsers in Java
- Difference between Externalizable and Serializable in Java
- Difference between Vector and ArrayList in java?
- Difference between Comparator and Comparable in Java
- Difference between TreeMap and TreeSet in Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- Difference between Comparator and Comparable in java
- Difference between Stack and Heap in Java
- Difference between ArrayList and Vector In java
- Difference between HashMap and Hashtable in Java
- Difference between Enumeration and Iterator in Java
- Difference between HashSet and HashMap in Java
- Difference between parseInt and valueOf in java?
- Difference between ArrayList and LinkedList in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Java - Difference between Stack and Heap memory in Java
- Java - Difference between Wait and Sleep , Yield in Java
- 随机产生50个数字,每个数字的范围是[10, 50],统计每个数字出现的次数
- mathtype格式黏贴
- OPENGL固定图形渲染管线操作细节
- STL概述
- 平面矩形与圆相交(重叠)的判断
- Difference between DOM and SAX Parsers in Java
- 编辑距离
- 树复选框点击勾选,父节点勾选全部子节点,结合玄龙戏珠无级树,节点.Tag = (DataRow)数据列情景应用,同时限制树节点数据日期类型发生错误
- How to Parse or Read XML File in Java >> XML Tutorial Example
- Wireshark自定义协议RPUDPDL(UDP下载)插件 (lua版)
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- JavaScript跨域插件 实现双向跨域
- How to Create and Evaluate XPath Expression in Java - Tutorial and Example
- 一些cocos2d-x开发的知识点总结