I am trying to parse XML data in the format shown below, using ElementTree:
<dataset>
<title>Birds of Kafiristan</title>
<creator>
<individualName>
<givenName>James</givenName>
<surName>Brooke</surName>
</individualName>
</creator>
<creator>
<organizationName>Bird Conservation Alliance</organizationName>
<address>
<deliveryPoint>P.O. Box 999</deliveryPoint>
<deliveryPoint>Mailstop 1234</deliveryPoint>
<city>Washington</city>
<administrativeArea>DC</administrativeArea>
<postalCode>9999</postalCode>
<country>USA</country>
</address>
<phone phonetype="voice">999-999-9999 x 123</phone>
<phone phonetype="fax">999-999-9999</phone>
<electronicMailAddress>[email protected]</electronicMailAddress>
<onlineUrl>http://www.birds.org/</onlineUrl>
</creator>
<contact>
<individualName>
<givenName>Josiah</givenName>
<surName>Harlan</surName>
</individualName>
</contact>
<pubDate>2010</pubDate>
<abstract>
<para>This dataset contains the results of a bird survey from Kafiristan</para>
</abstract>
<keywordSet>
<keyword>birds</keyword>
<keyword>biodiversity</keyword>
<keyword>animal ecology</keyword>
</keywordSet>
<distribution>
<online>
<url>http://birds.org/datasets</url>
</online>
</distribution>
</dataset>
(Indeed this is just a fragment of a much larger dataset, which includes other tags, but it will suffice to ask my question.)
I want simply to get the values of the elements for each tag, using code like:
from xml.etree import ElementTree as ET
rootElement = ET.parse("example.xml").getroot()
for subelement in rootElement:
for subsub in subelement:
print subsub.tag,"-->", subsub.text #, subsub.attrib, subsub.items()
for subsubsub in subsub:
print subsubsub.tag, "-->", subsubsub.text
Ruiing the code snippet above, I get the values of some elements, but not all -- indeed, I cannot get the values for nested elements (as "givenName" and "surName", which are nested inside "individualName", which in turn is nested into "creator").
Any hints or tips?
As always, thanks in advance for any assistance you can provide1
Element.findmight or perhapsElement.iterbe helpful...