views:

224

answers:

2

My application composes a webpage model from a number of xml sources. These sources are being parsed into memory as DOM objects with the normal Xerces parser. Unfortunately, Xerces DOM objects are not thread safe for read-only operations. I would like to be able to reuse the parsed DOM for read. Does anyone know of another parser or a simple thread safe for read DOM implementation that I use?

A: 

I don't know any perfect and simple solution.

An idea might be to recreate the Dom using thread-safe objects.

In this case, they would preferably be immutable, as you are reading-only. Being immutable also opens possibilities for further improvements (instance sharing for example, that would lead to smaller memory footprint).

I wish I could suggest a library that does this, as it is a fair amount of coding...

KLE
+1  A: 

From the description of your problem it sounds like to wish to construct the DOM once and then potentially perform read operations from multiple threads; i.e. the structure is built once and then never modified.

Assuming this is the case the only thing you need to ensure is that the thread creating the DOM either:

  • Assigns it to a volatile instance variable. This ensures that the DOM created has not been cached locally by the creating thread but rather written to main memory.
  • Assigns it to an instance variable guarded by a appropriate locks (e.g. synchronized getters and setters).
  • Assigns it to a final instance variable (e.g. within a constructor).

Disclaimer: My answer assumes that reading from the Xerces DOM does not alter it in any way. Can you provide a citation for your claim that Xerces is not thread-safe for read-only operations?

Adamski
Steven Huwig