How do I extract value of XML attribute in Python?

Tags: ,



I need to extract the value of an attribute in an XML document using Python.

For example, If I have an XML document like this:

<xml>
    <child type = "smallHuman"/>
    <adult type = "largeHuman"/>
</xml>

How would I be able get the text ‘smallHuman’ or ‘largeHuman’ to store in a variable?

Edit: I’m very new to Python and may require a lot of assistance.

This is what I’ve tried so far:

#! /usr/bin/python

import xml.etree.ElementTree as ET


def walkTree(node):
    print node.tag
    print node.keys()
    print node.attributes[]
    for cn in list(node):
        walkTree(cn)

treeOne = ET.parse('tm1.xml')
treeTwo = ET.parse('tm3.xml')

walkTree(treeOne.getroot())

Due to the way this script will be used, I cannot hard-code the XML into the .py file.

Answer

Using ElementTree you can use find method & attrib .

Example:

import xml.etree.ElementTree as ET

z = """<xml>
    <child type = "smallHuman"/>
    <adult type = "largeHuman"/>
</xml>"""


treeOne = ET.fromstring(z)
print treeOne.find('./child').attrib['type']
print treeOne.find('./adult').attrib['type']

Output:

smallHuman
largeHuman


Source: stackoverflow