将大型 xml 文件拆分为子文件而不会出现内存争用 [英] Splitting large xml files in to sub files without memory contention

查看:23
本文介绍了将大型 xml 文件拆分为子文件而不会出现内存争用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 XML

I have a XML like following

<Jobs>
   <job>
   ....
   </job>
   <job>
   ....
   </job>
   ....
</Jobs>

现在最好的方法是将每个作业节点写入一个单独的文件而不带整个使用 xmlreader 和 xmlwriter 或任何其他选项将文件写入内存?

Now what is best way to write each job node in a separate file without bringing the whole file in to memory using xmlreader and xmlwriter or anyother options?

推荐答案

  1. 为输入文件创建一个 XmlReader.
  2. 将读者定位在第一个工作元素上.
  3. 使用 ReadSubtree 方法.
  4. 为输出文件创建一个 XmlWriter.
  5. 使用 WriteNode 方法
  6. 将子树 XmlReader 的内容复制到 XmlWriter一>.
  7. 将原始阅读器放在下一个作业元素上,继续第一个作业元素.
    如果没有更多工作元素可供阅读,则中断.
  1. Create an XmlReader for the input file.
  2. Position the reader on the first job element.
  3. Create a subtree XmlReader using the ReadSubtree Method.
  4. Create an XmlWriter for the output file.
  5. Copy the contents of the subtree XmlReader into the XmlWriter using the WriteNode Method.
  6. Position the original reader on the next job element, continue as with the first job element.
    Break if there are no more job elements to read.

这篇关于将大型 xml 文件拆分为子文件而不会出现内存争用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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