3

Processing XML documents in java using DOM can be overly memory intensive for document that contain a large number of repetitive nodes, attributes, or values.

Does anyone know of an Java XML DOM API that leverages the flyweight-pattern internally to keep memory footprint to a minimum ? Maybe even configurable / enable / disable ?

Chris Johnson
  • 2,631
  • 2
  • 18
  • 17
  • I doubt that there is any DOM-like implementation with a flyweight pattern as changing values in the tree (which is DOM about) is very difficult to achieve then. – Uwe Plonus Jul 11 '13 at 12:55
  • Or as long as the user understood that since the nodes were the same changing one would change them all, shared resources is what the flyweight is about. DOM assumes everything is unique, and that's not always the case. – Chris Johnson Jul 11 '13 at 13:51

3 Answers3

1

Did you really mean the flyweight pattern? Or did think about something like lazy loading (or maybe "lazy parsing")? Using the flyweight pattern would require the parser to recognize elements that have the same content (and structure). That would be time consuming operation (and I fear, it would be space consuming also). Additionally, at which depth should the parser begin to recognize flyweights?

In fact, I can't imagine there is any DOM parser recognizing flyweights. And there also will be no DOM parser using lazy loading. This is the nature of DOM: Parse the whole XML file and produce a data structure for a highly flexible and random navigation.

Maybe you could indeed switch to a streaming process by using SAX or StAX. Of course you would lose the ability of flexible and random navigation, as you now must process you elements "on the fly".

However, there is a parser called VTD-XML. This was developped with memory efficiency in mind. It also loads the whole XML file into memory and provides some methods for navigating through the elements. But be warned: This navigation is somewhat complicated and not very intuitive (due to the nature of memory efficient storing). But maybe this is something you could use.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

I don't know of such a DOM parser (doesn't mean it doesn't exist). However, maybe a StAX parser would be close to what you are searching.

-1

It is not DOM, but SAX(javax.xml.parsers.SAXParser) should be helpful.

Parsing an XML File Using SAX

  • That's not an answer to the question asked. DOM has some properties that SAX does not have (e.g. navigation). – Uwe Plonus Jul 11 '13 at 12:49