Created
September 2, 2011 12:33
-
-
Save flavianmissi/1188501 to your computer and use it in GitHub Desktop.
Parsing a xml string (or file) with Python's xml.etree module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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