Python XML 问题

发布于 2024-12-04 09:18:44 字数 823 浏览 0 评论 0原文

我有一个 str 形式的 XML 文档。现在,在 XSD 中 是无界的,虽然大多数时候只有 1 个,但可能更多。我正在尝试使用 ElementTree,但遇到了一个问题:

>>> from xml.etree.ElementTree import fromstring
>>> 
>>> xml_str = """<?xml version="1.0"?>
... <foo>
...     <bar>
...         <baz>Spam</baz>
...         <qux>Eggs</qux>
...     </bar>
... </foo>"""
>>> # Try to get the document
>>> el = fromstring(xml_str)
>>> el.findall('foo')
[]
>>> el.findall('bar')
[<Element 'bar' at 0x1004acb90>]

显然,我需要循环遍历 ,但是因为 <; foo> 位于根目录,我不能。显然,我可以创建一个名为 的元素并将 el 放入其中,但是有没有更正确的方法来做到这一点?

I have an XML document as a str. Now, in the XSD <foo> is unbounded, and while most of the time there is only 1, there COULD be more. I'm trying to use ElementTree, but am running into an issue:

>>> from xml.etree.ElementTree import fromstring
>>> 
>>> xml_str = """<?xml version="1.0"?>
... <foo>
...     <bar>
...         <baz>Spam</baz>
...         <qux>Eggs</qux>
...     </bar>
... </foo>"""
>>> # Try to get the document
>>> el = fromstring(xml_str)
>>> el.findall('foo')
[]
>>> el.findall('bar')
[<Element 'bar' at 0x1004acb90>]

Clearly, I need to loop through the <foo>s, but because <foo> is at the root, I can't. Obviously, I could create an element called <root> and put el inside of it, but is there a more correct way of doing this?

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

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

发布评论

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

评论(2

机场等船 2024-12-11 09:18:44

每个 XML 文档都应该有一个 根元素。如果您想支持多个 foo 元素,则需要调整 XML。

Each XML document is supposed to have exactly one root element. You will need to adjust your XML if you want to support multiple foo elements.

原野 2024-12-11 09:18:44

唉,用 tree = ElementTree(el) 将元素包装在 ElementTree 中,并尝试 tree.findall('//foo') 却不行。似乎也不起作用(似乎您只能在元素“下方”搜索,即使搜索是从完整树中完成的,它也会在根“下方”搜索)。由于 ElementTree 并未声称真正实现了 xpath,因此很难说这是有意为之还是一个错误。

解决方案:不使用具有完整 xpath 支持的 lxml (el.xpath('//foo')例如),最简单的解决方案是使用 Element.iter() 方法。

for foo in el.iter(tag='foo'):
    print foo

或者如果您想要列表中的结果:

list(el.iter(tag='foo'))

请注意,您不能以这种方式使用复杂路径,只需查找具有特定标记名的所有元素,从该元素开始(并包括该元素)。

Alas, wrapping the element in an ElementTree with tree = ElementTree(el) and trying tree.findall('//foo') doesn't seem to work either (it seems you can only search "beneath" an element, and even if the search is done from the full tree, it searches "beneath" the root). As ElementTree doesn't claim to really implement xpath, it's difficult to say whether this is intended or a bug.

Solution: without using lxml with full xpath support (el.xpath('//foo') for example), the easiest solution would be to use the Element.iter() method.

for foo in el.iter(tag='foo'):
    print foo

or if you want the results in a list:

list(el.iter(tag='foo'))

Note that you can't use complex paths in this way, just find all elements with a certain tagname, starting from (and including) the element.

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