Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

xml.etree.ElementTree.Element' object has no attribute 'getchildren' and relative XPath does not find attributes

Ask Question

I am not able parse and print elements and attributes from a collection category from web page using atom-xml. From the Python script below, the "print(response.content)" lists contents ok and includes:

"xmlns= <atom:category scheme="http://www.zooo.com/types" 
term="changetimber/has-game"/>
And prints child tags ok:
{http://www.w3.org/2007/app}workspace
{http://www.w3.org/2005/Atom}title
{http://www.w3.org/2007/app}collection
{http://www.w3.org/2005/Atom}title
{http://www.w3.org/2007/app}categories
{http://www.w3.org/2005/Atom}category

After the Python script py is ran, get error children = hasgame.getchildren AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren'

And does not print anything from the for condition below: for category in root.findall('''.//@term="changetimber/hasgame" ] '''): print(category.attrib)

The Python script below is:

    import requests
    from requests.auth import HTTPBasicAuth
    import xml.etree.cElementTree as ET
    url = 'http://ipaddress/mygame'
    response = requests.get(url, auth=HTTPBasicAuth('user','pass'))
    print(response)
    print("***********list response content ******************")
    print(response.content)
    print("******** Display child tags********")
    root = ET.fromstring(response.content)
    for child in root.iter('*'):
    print(child.tag)
    # Create a dictionary
    xmlDictionary = {}
    for hasgame in root:
    children = hasgame.getchildren()
    xmlDictionary[children[0].text] = children[1].text
    print("******** print dictionary ******")
    print(xmlDictionary)
    for category in root.findall('''.//@term="changetimber/hasgame" ] '''):
       print(category.attrib)
                Hi @nasiajai. I used "hasname" as an example. The actual atom xml page contains"has-game", with hyphen. When I used "has-game" in the for condition, and run the .py script, get this error: SyntaxError: cannot assign to expression. To try to fix, I created a variable as hasgame = "has-game". Then, use "hasgame" in the for condition. Next, I ran the .py script again. But get errors IndexError: list index out of range for my statement: xmlDictionary[children[0].text] = children[1].text in the script. Thank you.
– Ben Q
                May 13, 2022 at 2:27
                Just to add a little more info about the reason here: xml.etree.ElementTree.Element used to have a method called getchildren(), so you'll see existing code that uses it. It was deprecated in version version 3.2 and then removed in 3.9 (see docs.python.org/3.8/library/…)
– NateW
                Jun 21, 2022 at 15:31
                Hi, it don't really work the same way, the children being sometime XML element it will alway display a one element list
– SylwekFr
                Oct 27, 2022 at 8:15
                Hi @asiajai, SklwekFr thank you for the inputs. I agree with both of your answers. It was challenging situation and I was not able to solve.
– Ben Q
                Oct 30, 2022 at 18:38
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.