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.
To get the attribute value from an XML, you can do like this:
import xml.etree.ElementTree as ET
xml_data = """<xml>
<child type = "smallHuman"/>
<adult type = "largeHuman"/>
</xml>"""
# This is like ET.parse(), but for strings
root = ET.fromstring(xml_data)
for a child in root:
print(child.tag, child.attrib)
You can find more details and examples on the link below: https://docs.python.org/3.5/library/xml.etree.elementtree.html
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With