Get attribute names and values from ElementTree

后端 未结 3 1894
爱一瞬间的悲伤
爱一瞬间的悲伤 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:22

    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...

    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
    

提交回复
热议问题