I'm a complete beginner, and have some problems trying to open an XML-file from an URL using Python.
Here's my code (a snippet i found on the web):
# import library to do http requests:
from urllib.request import urlopen
#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations
#download the file:
file = urlopen('http://www.odaa.dk/storage/f/2014-04-28T12%3A49%3A26.677Z/lejemaal.xml')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('tagName')[0].toxml()
#strip off the tag (<tag>data</tag> ---> data):
xmlData = xmlTag.replace('<tagName>', '').replace('</tagName>', '')
#print out the xml tag and data in this format: <tag>data</tag>
print(xmlTag)
#just print the data
print(xmlData)
When I run this, I get an error saying:
Traceback (most recent call last):
File "/Users/-----/PycharmProjects/First/test.py", line 20, in <module>
xmlTag = dom.getElementsByTagName('tagName')[0].toxml()
IndexError: list index out of range
Having read in similar threads here on the board, it seems like I'm trying to access something that doesn't exist. Or is it because the snippet I copied says "tagName"? Do I need to edit this?
How do I solve my problem? I'm not even sure what result I'm fishing for, as I'm just trying to get something to happen. Hopefully someone can point me in the right direction :)