I am new to xml parsing. This xml file has the following tree:
FHRSEstablishment
|--> Header
| |--> ...
|--> EstablishmentCollection
| |--> EstablishmentDetail
| | |-->...
| |--> Scores
| | |-->...
|--> EstablishmentCollection
| |--> EstablishmentDetail
| | |-->...
| |--> Scores
| | |-->...
but when I access it with ElementTree and look for the child
tags and attributes,
import xml.etree.ElementTree as ET
import urllib2
tree = ET.parse(
file=urllib2.urlopen('http://ratings.food.gov.uk/OpenDataFiles/FHRS408en-GB.xml' % i))
root = tree.getroot()
for child in root:
print child.tag, child.attrib
I only get:
Header {}
EstablishmentCollection {}
which I assume means that their attributes are empty. Why is it so, and how can I access the children nested inside EstablishmentDetail
and Scores
?
EDIT
Thanks to the answers below I can get inside the tree, but if I want to retrieve values such as those in Scores
, this fails:
for node in root.find('.//EstablishmentDetail/Scores'):
rating = node.attrib.get('Hygiene')
print rating
and produces
None
None
None
Why is that?
for child in root.find('.//Scores'): rating = child.get('Hygiene'); print rating;
I getNone
as a result. – Hyposensitize