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?
Saxon provides DOM wrappers to its internal and immutable data structure.
// create Saxon IdentityTransformer
final Transformer transformer = new TransformerFactoryImpl().newTransformer();
// set up holder for the output
final TinyBuilder outputTarget = new TinyBuilder(
new PipelineConfiguration(new Configuration()));
// transform into Saxon's immutable TinyTree
transformer.transform(xml, outputTarget);
// extract the whole XML as TinyNode
final TinyNodeImpl tinyNode = outputTarget.getTree().getNode(0);
// wrap TinyNode as DOM
final NodeOverNodeInfo nodeOverNodeInfo = DocumentOverNodeInfo.wrap(tinyNode);
// cast to DOM
final Document doc = (Document) nodeOverNodeInfo;
(tested with saxon-he 9.5.1)
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...
来源:https://stackoverflow.com/questions/1504571/creating-a-dom-that-is-thread-safe-for-read-operations