Why is sax parser faster than dom
Servlet Architecture. Session implementation. Soft Skills. Software Architecture. Software development process. Some Important java tips. Spiral model software development. Spring Configuration Files. Spring Hibernate Integration.
Spring Web flow. Spring Web MVC framework. SQL Joins. Stored procedureand functions. String vs StringBuffer. Struts framework. Struts Spring Hibernate. Sun Basic Code Conventions. The Do's and Don'ts of Java Programming. Thread vs Process. Top Ten Errors Java Programmers make. Tricky Method Overloading. Two-phase commit. V-Model software development. Waterfall model software development. Web Services. What is the difference between. What this site will do for you? Why J2EE?
Why Java? Why Struts Framework. Why wait , notify and notifyAll. Both has advantages and disadvantages and can be used in our programming depending on the situation. SAX: 1. Parses node by node 2. Doesnt store the XML in memory 3. We cant insert or delete a node 4. Top to bottom traversing DOM 1.
Stores the entire XML document into memory before processing 2. Occupies more memory 3. We can insert or delete nodes 4.
With Dom we can modify data. To parse human readable documents DOM is useful. Processing can be divided up into a chain of successive operations. The internal data structures are almost as complicated as the document itself. The program must modify the document repeatedly. SAX is an event based parser. Ideally a good parser should be fast time efficient ,space efficient, rich in functionality and easy to use. But in reality, none of the main parsers have all these features at the same time.
For example, a DOM Parser is rich in functionality because it creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree , but it is space inefficient when the document is huge, and it takes a little bit long to learn how to work with it.
A SAX Parser, however, is much more space efficient in case of big input document because it creates no internal structure. But from the functionality point of view, it provides less functions which mean that the users themselves have to take care of more, such as creating their own data structures.
I think the answer really depends on the characteristics of your application and your current requirements. For example, if your application needs to work on two XML documents, and does different things on each document, you could use a DOM parser on one document and a SAX parser on another, and then combine the results or make the processing cooperate with each other.
Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free. A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python. About Me. Active Oldest Votes. Assuming you do nothing but parse the document, the ranking of the different parser standards is as follows: 1.
StAX is the fastest The event is reported to you 2. SAX is next It does everything StAX does plus the content is realized automatically element name, namespace, attributes, It integrates cleanly with XSLT transforms javax. Improve this answer. Um, what do you think happens when "The event is reported to you" versus "the content is realized automatically"?
StAX will report that element is started, if you never ask for the element name or URI then that data need not ever be realized as String objects. On the other hand a SAX parser will realize that data as String objects as part of the event. And if you tell me that you've looked at the internals of StaX and it's built around a character-based state machine, I'll believe you. However, I would expect it to generate tokens internally, even if you never ask for them.
I have never looked at the internals of a StAX parser. If you only consider the namespace URI aspect. A realized token could be "foo:bar". Add a comment. My question was "why is sax parsing faster? Stargazer - mikerobi's answer did not address my question. I have an open mind if someone would put in the time to provide a thoughtful answer.
To me it is pretty obvious that a process that requires more memory, and will not give you access to the data until it is completely parsed will be slower than a process that requires very little memory and allows you to access data almost as fast as it can be read. Given that, mikerobi's answer is justified. You may indeed understand the difference, but your question doesn't make that clear.
Alleging that he didn't read the question or gave a thoughtless answer is unnecessary. I got a bit defensive because I was charged with not having an "open mind". I do a lot more memory intensive operations in a lot less time. Show 1 more comment. I can't really comment on StAX since I've never played with it. Buhake Sindi Buhake Sindi 84k 27 27 gold badges silver badges bronze badges.
0コメント