使用ElementTree解析具有名称空间的XML字符串 [英] Using ElementTree to parse an XML string with a namespace
问题描述
我已经把我的裤子脱掉了,但没有结果。我想做的非常简单:我想使用ElementTree访问字符串中包含的以下XML中的UniqueID值。
I have Googled my pants off to no avail. What I am trying to do is very simple: I'd like to access the UniqueID value in the following XML contained in a string using ElementTree.
from xml.etree.ElementTree import fromstring
xml_string = """<ListObjectsResponse xmlns='http://www.example.com/dir/'>
<Item>
<UniqueID>abcdefghijklmnopqrstuvwxyz0123456789</UniqueID>
</Item>
</ListObjectsResponse>"""
NS = "http://www.example.com/dir/"
tree = fromstring(xml_string)
我知道我应该使用 fromstring
方法来解析XML字符串,但我似乎无法识别如何访问UniqueID。我不确定如何使用 find
, findall
或 findtext $相对于名称空间的c $ c>方法。
I know that I should use the fromstring
method to parse the XML string, but I can't seem to identify how to access the UniqueID. I'm not certain how to use the find
, findall
, or findtext
methods with respect to the namespace.
任何帮助都是值得赞赏的。
Any help is totally appreciated.
推荐答案
以下内容将助您一臂之力:
The following should get you going:
>>> tree.findall('*/*')
[<Element '{http://www.example.com/dir/}UniqueID' at 0x10899e450>]
这将列出位于树根以下两级的所有元素(在您的情况下为UniqueID元素)。您也可以使用 tree.find()
在此级别仅找到 first 元素。然后,您可以直接获取UniqueID元素的文本内容:
This lists all the elements that are two levels below the root of your tree (the UniqueID element, in your case). You can, alternatively, find only the first element at this level, with tree.find()
. You can then directly get the text contents of the UniqueID element:
>>> unique_id_elmt = tree.find('*/*') # First (and only) element two levels below the root
>>> unique_id_elmt
<Element '{http://www.example.com/dir/}UniqueID' at 0x105ec9450>
>>> unique_id_elmt.text # Text contained in UniqueID
'abcdefghijklmnopqrstuvwxyz0123456789'
或者,您可以直接找到通过指定完整路径:
Alternatively, you can directly find some precise element by specifying its full path:
>>> tree.find('{{{0}}}Item/{{{0}}}UniqueID'.format(NS)) # Tags are prefixed with NS
<Element '{http://www.example.com/dir/}UniqueID' at 0x10899ead0>
如Tomalak所说, Fredrik Lundh的网站可能包含有用的信息;您想检查前缀的处理方式:实际上,可能有一种比在前缀中显式表示 NS
路径更简单的方法来处理它们。上面的方法。
As Tomalak indicated, Fredrik Lundh's site might contain useful information; you want to check how prefixes can be handled: there might in fact be a simpler way to handle them than by making explicit the NS
path in the method above.
这篇关于使用ElementTree解析具有名称空间的XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!