Jan-22-2020, 10:53 AM
Hi all,
I just learned the python basics and so I'm pretty sure that I have overseen something pretty obvious. Due to the fact that I'm trying to find a solution for hours now, I'm writing this post.
TL;DR: When I add an xmlns attribute to my xml file, the root.findall function doesn't work anymore. '{namespace}condition' shows me a no attribute error.
It's my very first project where I try to read data from an xml file. For getting a deeper understanding, I'm using the XML examlple from docs.python.org which looks like that:
Reading the documentation, I saw that I need to put the namespace into my for loop:
I searched the internet for hours now, found several threads, tried the same with lxml, etc but somehow I don't get the point here. Could anyone explain what I'm doing wrong here?
Regards,
Toni
I just learned the python basics and so I'm pretty sure that I have overseen something pretty obvious. Due to the fact that I'm trying to find a solution for hours now, I'm writing this post.
TL;DR: When I add an xmlns attribute to my xml file, the root.findall function doesn't work anymore. '{namespace}condition' shows me a no attribute error.
It's my very first project where I try to read data from an xml file. For getting a deeper understanding, I'm using the XML examlple from docs.python.org which looks like that:
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> ... </data>By following the documentation, the following for loop shows the expected results:
for country in root.findall('country'): rank = country.find('rank').text print(rank)But when I add an xmlns Attribute to the first line of my XML file (==> <data xmlns="http://foo.com">), there are no search results anymore.
Reading the documentation, I saw that I need to put the namespace into my for loop:
for country in root.findall('{http://foo.com}country'): rank = country.find('rank').text ...But doing so I recive an AttributeError from line 2 of our example, saying "AttributeError: 'NoneType' object has no attribute 'text'
I searched the internet for hours now, found several threads, tried the same with lxml, etc but somehow I don't get the point here. Could anyone explain what I'm doing wrong here?
Regards,
Toni