Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. SAXModelBuilder vs DOM Parser

SAXModelBuilder vs DOM Parser

Scheduled Pinned Locked Moved XML / XSL
htmlquestioncssvisual-studiocom
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Josh Owen
    wrote on last edited by
    #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[^]

    L 1 Reply Last reply
    0
    • J Josh Owen

      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[^]

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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?

      J 1 Reply Last reply
      0
      • L led mike

        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?

        J Offline
        J Offline
        Josh Owen
        wrote on last edited by
        #3

        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...

        L 1 Reply Last reply
        0
        • J Josh Owen

          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...

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          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?

          J 1 Reply Last reply
          0
          • L led mike

            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?

            J Offline
            J Offline
            Josh Owen
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups