Skip to content

Instantly share code, notes, and snippets.

@flavianmissi
Created September 2, 2011 12:33
Show Gist options
  • Save flavianmissi/1188501 to your computer and use it in GitHub Desktop.
Save flavianmissi/1188501 to your computer and use it in GitHub Desktop.
Parsing a xml string (or file) with Python's xml.etree module
xml_str = """<root>
<row><total>39</total><id_pais>BR </id_pais><id_estado>PE </id_estado>
<cidade>NADA</cidade><cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
<row><total>39</total><id_pais>BR </id_pais><id_estado>CE </id_estado><cidade>NADA</cidade>
<cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
<row><total>39</total><id_pais>BR </id_pais><id_estado>SC </id_estado><cidade>NADA</cidade>
<cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
<row><total>39</total><id_pais>BR </id_pais><id_estado>SP </id_estado><cidade>NADA</cidade>
<cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
</root>"""
xml_tree = ElementTree.fromstring(xml_str) # <Element 'root' at 0x100567e90>
row = xml_tree.find('row')
row.getchildren()[0].text # '39'
for child in xml_tree.iter():
print child.tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment