Python XML 问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个 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.唉,用
tree = ElementTree(el)
将元素包装在ElementTree
中,并尝试tree.findall('//foo')
却不行。似乎也不起作用(似乎您只能在元素“下方”搜索,即使搜索是从完整树中完成的,它也会在根“下方”搜索)。由于 ElementTree 并未声称真正实现了 xpath,因此很难说这是有意为之还是一个错误。解决方案:不使用具有完整 xpath 支持的 lxml (
el.xpath('//foo')
例如),最简单的解决方案是使用 Element.iter() 方法。或者如果您想要列表中的结果:
请注意,您不能以这种方式使用复杂路径,只需查找具有特定标记名的所有元素,从该元素开始(并包括该元素)。
Alas, wrapping the element in an
ElementTree
withtree = ElementTree(el)
and tryingtree.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.or if you want the results in a list:
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.