libxml2 编写器差异

发布于 2025-01-05 04:51:52 字数 330 浏览 1 评论 0原文

我能找到的大部分 libxml2 示例都是关于加载/解析 XML 文件的。但我只对写它们感兴趣;该代码永远不需要解析任何文件。有一个示例使用不同的编写器,其中显示了如何使用文件、内存DOM 模型。

仔细查看代码,我没有发现它们在编写方面有任何显着差异。如何决定使用哪一个更好? (换句话说,在什么情况下一个人比​​其他人更好?)

The bulk of the examples I can find for libxml2 are all about loading/parsing XML files. But I'm only interested in writing them; the code will never have to parse any files. There is an example using different writers, where it shows how to use the file, memory, DOM and tree models.

Looking through the code, I don't see any significant differences between them when it comes to writing. How does one decide which is better to use? (In other words, in what cases is one better than the others?)

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

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

发布评论

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

评论(2

情话已封尘 2025-01-12 04:51:52

您指定的 4 个函数之间的差异很小,主要取决于内容的去向。正如 Alex 提到的,如果内存是一个问题,使用 xmlNewTextWriterFilename 的优点是不需要将结果保存在内存中。

您提到的所有方法都属于 xmlWriter API,它是提供的 API 之一。另一个值得注意的是 tree API。 xmlWriter 更像是调用 write() 来打印到文件,而树更像是在内存中构建嵌套结构。

如果您的数据以非线性方式构建,可以返回并根据后来的信息添加/更改内容等,那么基于树的版本可能会很好。这将需要使用流式 xmlWriter 接口进行一些解决方法/缓存,因为您可以一旦输出就不要改变。然而,内存中的树可以完全调整,直到它被序列化为止。

树 API 的缺点是它必须将整个事物保留在内存中;经验法则是解析树的内存需求大约是序列化 xml 文件大小的 4 倍。

我的决定通常取决于我是否希望创建大型文档。如果没有,我会使用 if the tree api,因为如果我想要的话,灵活性就会存在。如果我知道效率会成为一个问题或者我将处理大量内容,那么流式 xmlWriter 就是最佳选择。

树 API 示例可以在这里找到: http://xmlsoft.org/examples/index.html#Tree

The differences between the 4 functions you specify are minimal, it's all about where the contents go. As Alex mentioned, if memory is a concern, using xmlNewTextWriterFilename has the advantage of not needing to hold the result in memory.

The xmlWriter API, to which all the methods you mentioned belong, is one of the APIs offered. The other of note is the tree API. xmlWriter is more like calling write() to print to a file, and the tree is more like building nested structs in memory.

The tree-based versions can be good if your data is constructed in a non-linear fasion, going back and adding/changing things based on later information, etc. This would require some workarounds/caching with the streaming xmlWriter interface, as you can't change things once they've been output. The in-memory tree, however, can be fully tweaked until the instant it's serialized.

The tree API has the downside of the fact it has to keep the entire thing im memory; the rule of thumb is the memory requirements for a parsed tree is rougly 4x the size of serialized xml file.

My decision is usually dependent on whether I expect to create large documents. If not, I use the if the tree api, as the flexibility will be there if I want it. If I know efficiency will be a concern or I'll be working with large stuff, the streaming xmlWriter is the way to go.

tree API examples can be found here: http://xmlsoft.org/examples/index.html#Tree

〃温暖了心ぐ 2025-01-12 04:51:52

如果您使用的设备内存有限,您可能不想使用 DOM 或基于内存的方法。在这种情况下,您可能希望在迭代要写入 XML 的数据结构时写出该文件。

If you're on a device with limited memory, you probably don't want to use DOM or memory-based approaches. In that case, you probably want to write out the file as you iterate through the data structure you want to write to XML.

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