Get attribute names and values from ElementTree

后端 未结 3 1937
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 05:28

I have an XML element with several attributes. I\'ve been using the ElementTree package.

After I\'ve parsed a tree from an xml fil

3条回答
  •  花落未央
    2021-02-08 06:15

    The attrib attribute of an ElementTree element (like the root returned by getroot) is a dictionary. So you can do, for example:

    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'}
    

提交回复
热议问题