Creating a DOM that is thread safe for read operations

≯℡__Kan透↙ 提交于 2019-12-05 11:06:49

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...

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