Python Forum
Beginner question: lxml's findall in an xml namespace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question: lxml's findall in an xml namespace
#1
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:
<?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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which namespace? Skaperen 5 292 May-28-2024, 05:38 AM
Last Post: Gribouillis
  Very Beginner question on simple variables Harvy 1 349 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  regex findall() returning weird result Radical 1 741 Oct-15-2023, 08:47 PM
Last Post: snippsat
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,300 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,854 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,488 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,649 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  'namespace' shorthand for function arguments? shadowphile 5 2,717 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Beginner question - storing values cybertron2 4 3,359 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,902 Oct-17-2020, 12:31 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020