Saturday, 31 August 2013

XML parsing using python (with xmlns attribute) doesn't work

XML parsing using python (with xmlns attribute) doesn't work

It's my first time trying to parse XML with python so answer could be
simple but I can't figure this out.
I'm using ElementTree to parse some XML file. Problem is that I cannot get
any result inside the tree when having this attribute:
<package xmlns="http://apple.com/itunes/importer" version="software5.1">
When removing this attribute everything works great. To be clear I mean
when changing first line of XML file to:
<package>
Everything works great.
What am I doing wrong?
Here is my code:
import xml.etree.ElementTree as ET
tree = ET.parse('metadataCopy.xml')
root = tree.getroot()
p = root.find(".//intervals/interval")
print p
for interval in root.iterfind(".//intervals/interval"):
start_date = interval.find('start_date').text
end_date = interval.find('end_date').text
print start_date, end_date
Please help. Thanks!

1 comment: