gpt4 book ai didi

c - 是否有可能克隆 xmlTextReader(或多遍阅读)?

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:53 25 4
gpt4 key购买 nike

我目前必须修复现有应用程序以使用 DOM interface 以外的东西的 libxml2因为事实证明它传递的 XML 文件太大以至于无法加载到内存中。

我已经将数据加载从遍历 DOM 树重写为使用 xmlTextReader在大多数情况下,现在没有太多问题。 (我使用 xmlNewTextReaderFilename 打开本地文件。)

然而事实证明,大数据所在的子树必须不按顺序读取,但我必须先收集一些(少量)数据。 (而问题恰恰是这个子树包含了大量的数据,所以只将这个子树加载到内存中也没有多大意义。)

最简单的方法就是“克隆”/“复制”我当前的阅读器,提前阅读,然后返回到原始实例继续阅读。 (似乎我是 not the first one ...甚至在 C# 端实现了一些东西:XML Reader with Bookmarks。)

除了“复制”xmlTextReader 的状态之外似乎没有任何其他方法。

如果我不能重新读取文件的部分,我也可以重新读取整个文件,这虽然很浪费,但在这里没问题,但我仍然需要记住我事先在哪里?

是否有一种简单的方法可以记住 xmlTextReader 在当前文档中的位置,以便我稍后可以在第二次读取文档/文件时再次找到该位置?

这是一个问题示例:

<root>
<cat1>
<data attrib="x1">
... here goes up to one GB in stuff ...
</data>
<data attrib="y2"> <!-- <<< Want to remember this position without having to re-read the stuff before -->
... even more stuff ...
</data>
<data attrib="z3">
<!-- I need (part of) the data here to meaningfully interpret the data in [y2] that
came before. The best approach would seem to first skip all that data
and then start back there at <data attrib="y2"> ... not having to re-read
the whole [x1] data would be a big plus! -->
</data>
</cat1>
...
</root>

最佳答案

我想根据我的 learned at the XML mailing list 给出解决方法:

没有简单的方法来“克隆” xmlReader 上的状态,但是应该可行并且应该相当简单的方法是计算对文档所做的读取。

也就是说,要使用 xmlReader 读取文档,您可能必须调用以下命令:

// looping ...
status = ::xmlTextReaderRead(pReader);

如果您以结构化的方式进行操作(例如,我最终编写了一个包装器类来封装我对 xmlReader 的使用模式),那么添加一个计数器就相对容易了:

// looping ...
status = ::xmlTextReaderRead(pReader);
if (1 == status) { // success
++m_ReadCounter;
}

要重新读取文档(到达某个位置),您只需调用 xmlTextReaderRead 多次 m_ReadCounter 次,丢弃结果,直到到达位置你想重新开始。

是的,您必须重新解析整个文档,但这可能已经足够快了。 (实际上可能比缓存文档的大量部分更好/更快。)

关于c - 是否有可能克隆 xmlTextReader(或多遍阅读)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com