I have an XML <root>
element with several attributes. I've been using the ElementTree
package.
我有一个带有多个属性的XML
After I've parsed a tree from an xml file, I'm getting the document root, but I want to get the requested attribute, or even the entire list of attributes.
在我从xml文件解析树之后,我得到了文档根目录,但我想获取所请求的属性,甚至是整个属性列表。
<root a="1" b="2" c="3">
</blablabla>
</root>
How can I retrieve all attribute names and values for a <root>
element with ElementTree?
如何使用ElementTree检索
3 个解决方案
#1
16
Each Element
has an attribute .attrib
that is a dictionary; simply use it's mapping methods to ask it for it's keys or values:
每个元素都有一个属性.attrib,它是一个字典;只需使用它的映射方法来询问它的键或值:
for name, value in root.attrib.items():
print '{0}="{1}"'.format(name, value)
or
要么
for name in root.attrib:
print '{0}="{1}"'.format(name, root.attrib[name])
or use .values()
or any of the other methods available on a python dict
.
或者使用.values()或python dict上可用的任何其他方法。
To get an individual attribute, use the standard subscription syntax:
要获取单个属性,请使用标准订阅语法:
print root.attrib['a']
#2
6
The attrib
attribute of an ElementTree element (like the root returned by getroot
) is a dictionary. So you can do, for example:
ElementTree元素的attrib属性(如getroot返回的根)是一个字典。所以你可以做,例如:
from xml.etree import ElementTree
tree = ElementTree.parse('test.xml')
root = tree.getroot()
print root.attrib
which will output, for your example
对于你的例子,它将输出
{'a': '1', 'b': '2', 'c': '3'}
#3
1
Some nice loop you can use it will get for each element of the xmlObject it's tag, text and attribute it will work for 2 levels XML, it's not the best way to iterate but it can be useful for simple things...
你可以使用它的一些好的循环将获得xmlObject的每个元素它的标签,文本和属性它将适用于2级XML,它不是迭代的最佳方式,但它对简单的事情有用...
for headTag in xmlObject.getchildren():
print headTag.tag, headTag.text, headTag.attrib
for bodyTag in headTag.getchildren():
print "\t", bodyTag.tag, bodyTag.text, bodyTag.attrib
#1
16
Each Element
has an attribute .attrib
that is a dictionary; simply use it's mapping methods to ask it for it's keys or values:
每个元素都有一个属性.attrib,它是一个字典;只需使用它的映射方法来询问它的键或值:
for name, value in root.attrib.items():
print '{0}="{1}"'.format(name, value)
or
要么
for name in root.attrib:
print '{0}="{1}"'.format(name, root.attrib[name])
or use .values()
or any of the other methods available on a python dict
.
或者使用.values()或python dict上可用的任何其他方法。
To get an individual attribute, use the standard subscription syntax:
要获取单个属性,请使用标准订阅语法:
print root.attrib['a']
#2
6
The attrib
attribute of an ElementTree element (like the root returned by getroot
) is a dictionary. So you can do, for example:
ElementTree元素的attrib属性(如getroot返回的根)是一个字典。所以你可以做,例如:
from xml.etree import ElementTree
tree = ElementTree.parse('test.xml')
root = tree.getroot()
print root.attrib
which will output, for your example
对于你的例子,它将输出
{'a': '1', 'b': '2', 'c': '3'}
#3
1
Some nice loop you can use it will get for each element of the xmlObject it's tag, text and attribute it will work for 2 levels XML, it's not the best way to iterate but it can be useful for simple things...
你可以使用它的一些好的循环将获得xmlObject的每个元素它的标签,文本和属性它将适用于2级XML,它不是迭代的最佳方式,但它对简单的事情有用...
for headTag in xmlObject.getchildren():
print headTag.tag, headTag.text, headTag.attrib
for bodyTag in headTag.getchildren():
print "\t", bodyTag.tag, bodyTag.text, bodyTag.attrib