SAXModelBuilder vs DOM Parser
-
I have a question about a SAX parser versus a DOM parser in terms of memory usage... I know that in terms of memory usage a SAX parser uses significantly less memory because of it's event based nature. However, in this example of a Model based SAX parser (see link below) it looks like this still reads the entire XML document into memory because of the way it creates instances of any classes that it needs to and builds lists of the objects. It seems to me that this would still be reading the entire document into memory like a DOM parser would. I may be wrong of course which is why I'm posting here... Any insite would be greatly appreciated... http://www.onjava.com/pub/a/onjava/excerpt/learnjava_23/index2.html?page=1[^]
-
I have a question about a SAX parser versus a DOM parser in terms of memory usage... I know that in terms of memory usage a SAX parser uses significantly less memory because of it's event based nature. However, in this example of a Model based SAX parser (see link below) it looks like this still reads the entire XML document into memory because of the way it creates instances of any classes that it needs to and builds lists of the objects. It seems to me that this would still be reading the entire document into memory like a DOM parser would. I may be wrong of course which is why I'm posting here... Any insite would be greatly appreciated... http://www.onjava.com/pub/a/onjava/excerpt/learnjava_23/index2.html?page=1[^]
Josh Owen wrote:
However, in this example of a Model based SAX parser
Josh Owen wrote:
It seems to me that this would still be reading the entire document into memory like a DOM parser would.
The example may well be doing that, but that is not the authors point, this is: What may be harder to see is how one could use SAX to build a real Java object model from an XML document. His example is how one could use a the SAX Parser to serialize the XML into a Java Object Model. What that model might be is completely up to you, and therefore does not have to be the entire document. Neither would it have to represent the XML, Node Types and Names etc., but rather your objects. Was that what you were asking?
-
Josh Owen wrote:
However, in this example of a Model based SAX parser
Josh Owen wrote:
It seems to me that this would still be reading the entire document into memory like a DOM parser would.
The example may well be doing that, but that is not the authors point, this is: What may be harder to see is how one could use SAX to build a real Java object model from an XML document. His example is how one could use a the SAX Parser to serialize the XML into a Java Object Model. What that model might be is completely up to you, and therefore does not have to be the entire document. Neither would it have to represent the XML, Node Types and Names etc., but rather your objects. Was that what you were asking?
Sure I understand that his point is to show how to build a Java Object Model using the SAX parser but my question is that he says this: "The primary motivation for using SAX instead of the higher-level APIs that we'll discuss later is that it is lightweight and event-driven. SAX doesn't require maintaining the entire document in memory." Then goes on to give that example when I'm fairly certain that example uses the same amount of memory a DOM parser would. But I may be wrong? So that's what I am trying to clarify. Do you think that example does actually use the same amount of memory as a DOM parser would? Thanks for the quick response...
-
Sure I understand that his point is to show how to build a Java Object Model using the SAX parser but my question is that he says this: "The primary motivation for using SAX instead of the higher-level APIs that we'll discuss later is that it is lightweight and event-driven. SAX doesn't require maintaining the entire document in memory." Then goes on to give that example when I'm fairly certain that example uses the same amount of memory a DOM parser would. But I may be wrong? So that's what I am trying to clarify. Do you think that example does actually use the same amount of memory as a DOM parser would? Thanks for the quick response...
Josh Owen wrote:
Do you think that example does actually use the same amount of memory as a DOM parser would?
I don't know, it is certainly possible. I don't understand the significance of that. The authors point, and it is a well known one, is that SAX parsers use less memory and are faster than DOM parsers. I have never studied the implementation of these parsers but the significant difference is that a DOM parser generates a DOM where a SAX parser does not. It is this generation of a DOM that consumes so much more memory and requires more processor instructions. However if you use a SAX parser to create the same information as a Document Object Model does then you would be re-creating the DOM parser scenario at which point there is no benefit in using SAX. The authors point is, if your model does not need the entire document structure then using SAX to generate your own model will be faster (and use less memory) than generating a DOM and pulling the information out of it to build your model. Does that help?
-
Josh Owen wrote:
Do you think that example does actually use the same amount of memory as a DOM parser would?
I don't know, it is certainly possible. I don't understand the significance of that. The authors point, and it is a well known one, is that SAX parsers use less memory and are faster than DOM parsers. I have never studied the implementation of these parsers but the significant difference is that a DOM parser generates a DOM where a SAX parser does not. It is this generation of a DOM that consumes so much more memory and requires more processor instructions. However if you use a SAX parser to create the same information as a Document Object Model does then you would be re-creating the DOM parser scenario at which point there is no benefit in using SAX. The authors point is, if your model does not need the entire document structure then using SAX to generate your own model will be faster (and use less memory) than generating a DOM and pulling the information out of it to build your model. Does that help?
Thanks, yeah I have no issue with what the author's point is. I understand what the differences between SAX and DOM are. I was going to recreate a DOM parser I had created as a SAX parser but I need the ability to jump around a little bit which is why I was going to use this Java Object Model. But once I started reading about it I was thinking that this may all be for naught because even though I'm using SAX it is stil storing everything in memory and therefore using the same amount of memory as a DOM parser. Thanks for the response