Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a DOM that is thread safe for read operations

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?

like image 995
Seth Weiner Avatar asked Oct 15 '22 13:10

Seth Weiner


1 Answers

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)

like image 75
Thomas Kiesling Avatar answered Oct 25 '22 12:10

Thomas Kiesling