-->

是否有克隆一个XmlTextReader(或多遍读)的可能性?(Is there a possibi

2019-10-17 21:36发布

我现在必须解决现有的应用程序使用比其他的东西DOM接口的libxml2的 ,因为事实证明,它被传递XML文件太大,不能被加载到内存中。

我从遍历DOM树使用重写的数据加载的XmlTextReader现在大部分没有太大的问题。 (我用xmlNewTextReaderFilename打开本地文件。)

事实证明,但是,在大数据所在的子树,必须按顺序读不懂,但我有收集一些(少量)之前,其他的数据。 (而问题恰恰在于它是这个子树包含大量的数据,所以只加载此子到内存中并没有太大的意义要么。)

最简单的事情是只“克隆” /“复制”我现在的阅读器,预读,然后返回到原来的实例继续阅读那里。 (看来我不是第一个 ......甚至还有一些在C#端实现: XML读卡器,带书签 )。

虽然目前没有出现任何方式然而,“复制”一个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>

Answer 1:

我想从我给出一个解决办法答案在XML邮件列表了解到 :

有没有简单的方法来“克隆”上一个XmlReader的状态,应该是什么但是可能也应该是很容易的计时过程中读取一个做了一个文件。

也就是说,读取与XMLReader的一个文件,你可能要调用以下:

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

只要你做,在一个结构化的方式(例如,我最后写一个小包装类,封装了的XmlReader我的使用模式),它是那么比较容易添加计数器:

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

对于重新读取文档(达到一定的位置),然后你只需要调用xmlTextReaderRead一些m_ReadCounter次,直到达到您想要重新开始的位置丢弃的结果。

是的,你必须重新分析整个文档,但可能是不够快。 (而实际上可能是更好/比缓存文件的体积非常大的一部分更快。)



文章来源: Is there a possibility for cloning an xmlTextReader (or multi-pass reading)?