创建一个对读操作线程安全的DOM [英] Creating a DOM that is thread safe for read operations

查看:135
本文介绍了创建一个对读操作线程安全的DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序组成了一个来自多个xml源的网页模型。使用普通的Xerces解析器将这些源解析为内存为DOM对象。不幸的是,对于只读操作,Xerces DOM对象线程安全。我希望能够重用已解析的DOM进行读取。有没有人知道我使用的读取DOM实现的另一个解析器或简单线程安全?

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为其提供DOM包装器内部和不可变数据结构。

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;

(用saxon-he 9.5.1测试)

(tested with saxon-he 9.5.1)

这篇关于创建一个对读操作线程安全的DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆