只解析 XML 文件的特定子树

发布于 2024-08-24 05:44:21 字数 358 浏览 7 评论 0原文

我有一个巨大的 XML 文件。然而,我只对这棵大树的一个小子树感兴趣。我想解析这个子树,但当我只使用它的一小部分时,我不想浪费时间解析整个庞大的树。

理想情况下,我想扫描整个文件,直到找到该子树的开头,解析子树直到到达它的结尾,然后甚至不去读取文件的其余部分。甚至可以用 XPath 表达式描述我感兴趣的子树。有没有办法使用 lxml 来做到这一点?看起来您可以使用 iterparse() 方法执行类似的操作,但根据文档,它看起来不会生成我想使用的已解析对象。有什么建议吗?

(不需要使用 lxml,但我想使用 Python,理想情况下我希望它速度快。)

I have a massive XML file. However, I'm only interested in a single small subtree of this massive tree. I want to parse this subtree, but I don't want to waste time parsing the entire massive tree when I'm going to only be using a small part of it.

Ideally, I'd want to scan through the file until I find the start of this subtree, parse the subtree until I reach the end of it, and then not even bother reading the rest of the file. Maybe even describe the subtree I'm interested in with an XPath expression. Is there a way to do this using lxml? It looks like you might be able to do something like this using the iterparse() method, but based on the docs, it looks like that doesn't produce a parsed object, which I want to use. Any suggestions?

(Using lxml is not required, but I want to use Python, and ideally I'd like it to be speedy.)

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

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

发布评论

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

评论(2

回眸一遍 2024-08-31 05:44:21

我的印象是 iterparse 就是你想要的。查看 http://lxml.de/parsing.html 中的“选择性标记事件”部分似乎这给了你你想要的:

context = etree.iterparse(xmlfile, tag="yourSubTree")
action, elem = context.next()
etree.iterwalk(elem, ...)...

似乎 XPath 也可以工作,但我猜 XPath 在返回之前会读取整个树,而我希望 iterparse 只会遍历树直到它有匹配项。值得对这两种方法进行分析。

I get the impression that iterparse is what you want. Looking at the section "Selective tag events" at http://lxml.de/parsing.html it seems like that gives you what you desire:

context = etree.iterparse(xmlfile, tag="yourSubTree")
action, elem = context.next()
etree.iterwalk(elem, ...)...

Seems like XPath could also work but I'd guess that XPath reads in the whole tree before returning whereas I'd expect iterparse to only walk the tree until it has a match. It would be worth profiling the two approaches.

⒈起吃苦の倖褔 2024-08-31 05:44:21

Iterparse 仍然需要将所有内容解析到您想要的子树。在使用正则表达式将子树输入解析器之前提取子树可能会更有效。您可能想尝试编写一个 sax 解析器。 Sax 可能比 lxml 慢,但它不会使用太多内存,因此在某些情况下它可能更好。

Iterparse will still require parsing everything up to the subtree you want. It might be more efficient to extract the subtree before you feed it into the parser with a regular expression. You might want to try writing a sax parser. Sax is probably slower than lxml, but it won't use much memory, so in some cases it might be better.

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