ElementTree返回Element而不是ElementTree [英] ElementTree Returns Element Instead of ElementTree

查看:178
本文介绍了ElementTree返回Element而不是ElementTree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从字符串构建 ElementTree 。当我执行以下操作时(如 Python ElementTree:解析字符串并获取ElementTree实例),我得到的是 Element 而不是 ElementTree

I am trying to build an ElementTree from a string. When I do the following (as set out in Python ElementTree: Parsing a string and getting ElementTree instance), I get an Element instead of an ElementTree:

companyTree = ElementTree.ElementTree(ElementTree.fromstring('<companies></companies>'))

如果我愿意

print(companyTree.getroot())

我得到


AttributeError:'xml.etree.ElementTree.Element'对象没有属性'getroot'

AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'

换句话说,companyTree是 Element 而不是 ElementTree 。根据 1 ,我应该得到一个ElementTree。 API是否已更改?我现在如何从字符串中获取ElementTree?谢谢。

In other words, companyTree is an Element instead of an ElementTree. According to the accepted answer in 1, I should be getting an ElementTree. Has the API changed? How do I get an ElementTree from a string now? Thanks.

请注意,在 Python ElementTree:ElementTree与根Element

推荐答案

这也让我有点难受。我有一些使用 parse()的旧代码。我重构为使用 fromstring(),导致相同的错误。
根据您的引用,我可以总结一下这里发生的事情;我相信这对于收到此特定错误的人将是有益的。其他文章直接涉及API本身,因此我不会将它们视为重复。

This one bit me too. I had some old code kicking around that used parse(). I refactored to use fromstring(), causing the same error. Based on your references, I can summarize what's going on here; I believe this will be beneficial for people getting this specific error. The other articles deal directly with the API itself so I wouldn't consider them duplicates.

因此,首先解决这点。您是正确的,所说的问题是相关的,但是我认为公认的答案并不是说 fromstring()应该返回 ElementTree 。相反,此答案的作者是说在问题的上下文中字符串等效于完整的XML文档而不是片段,因此将其转换为适当一个 ElementTree

So, first to address this point. You are correct that the stated issue is related, however I don't think the accepted answer is making the claim that fromstring() is supposed to return an ElementTree. Rather, the author of this answer is saying in the context of the problem the string is equivalent to a complete XML document rather than a fragment, so it's appropriate to convert it to an ElementTree:


当您使用ElementTree.fromstring()时返回基本上是树的根,因此,如果创建像ElementTree.ElementTree(root)这样的新树,就会得到所需的内容。

When you use ElementTree.fromstring() what you're getting back is basically the root of the tree, so if you create a new tree like this ElementTree.ElementTree(root) you'll get you're looking for.

在他的代码示例中,他只是这样做:

And in his code sample, he does just that:

from xml.etree.ElementTree import fromstring, ElementTree
tree = ElementTree(fromstring(<your_xml_string>))

这种方法由您发布的第二参考确认,该参考链接通过图书馆的作者,他在书中指出(和我的表述),这是不是设计怪癖,而是功能。期望像 parse()这样的结构被设计为可以处理完整的文档,而 fromstring()的结构被设计为可以工作。包含文档的片段。只是在后一种情况下,我们程序员才必须解决歧义,并通知库我们期望该字符串是一个完整的文档。

This approach is confirmed by the second reference you posted, which links an old article by the library's author, in which he states (and I paraphrase) that this is not a design quirk, it's a feature. The expectation is that constructs like parse() are designed to work with complete documents and fromstring() is designed to work with fragments and documents. It's just in this latter case that we the programmer must resolve the ambiguity and inform the library that we expect the string to be a complete document.

因此,就我的用例而言,我在XML字符串中有一个完整的文档,并且想要 ElementTree 功能,因此我包装了 fromstring () ElementTree 中进行调用,如上例所示。

So, as far as my use case goes, I had a complete document in my XML string and wanted the ElementTree features, so I wrapped my fromstring() call in an ElementTree as in the example above.

这篇关于ElementTree返回Element而不是ElementTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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