Here is the XML:
<top>
<target>
<name>TARGET_NAME_1</name>
<error_count>5</error_count>
<error_examples>a string goes here</error_examples>
</target>
<target>
<name>TARGET_NAME_2</name>
<error_count>5</error_count>
<error_examples>a string goes here</error_examples>
</target>
</top>
Here is what I’m trying:
tree = ETREE.parse(str(XML_FILE_PATH)) #this seems to work
top = tree.getroot()
targets = top.findall('target')
for target in targets:
print target
This gives me a <Element target at HEX_NUMBER>. So how do I extract the value of each target, i.e. TARGET_NAME_1 ?
Cheers
EDIT – I should mention that I’m on Python version 2.6
Say you want to print all names, you may do it like below:
Shorter to get name of first target:
But as it relies on item orders, and does not even check the item is of the right kind, you should probably not do that
Hence to get all the names, and only names, I would probably use a list comprehension as below: