创建一个对于读取操作线程安全的 DOM

发布于 2024-08-06 06:55:09 字数 163 浏览 11 评论 0原文

我的应用程序从多个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-08-13 06:55:09

Saxon 为其内部和不可变的数据结构提供 DOM 包装器。

// 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 测试)

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)

悲歌长辞 2024-08-13 06:55:09

我不知道有什么完美且简单的解决方案。

一个想法可能是使用线程安全对象重新创建 Dom

在这种情况下,它们最好是不可变,因为您是只读的。
不可变性还为进一步改进提供了可能性(例如,实例共享,这将导致更小的内存占用)。

我希望我能建议一个可以做到这一点的库,因为它需要大量的编码......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文