Creating a DOM that is thread safe for read operations

不羁的心 提交于 2019-12-07 04:06:38

问题


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?


回答1:


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)




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!