1

I have an XML file and I'd like to read some data out of it using Python's xml.etree.

Let's say that the XML file is like this :

<a>
   <b>
      <c>
         <d>This is the text I want to retrieve</d>
      </c>
   </b>
</a>

What I did was something like this :

document = ElementTree.parse('file.xml')
dToFind = document.find('d')
print(dToFind.text)

But it gave me the following error :

    print(dToFind.text)
AttributeError: 'NoneType' object has no attribute 'text'

What did I do wrong? And how can I fix it?

Thanks!

1 Answer 1

2

You can use XPATH for more sophesticated parsing combined with find for finding the node recursively

In this case:

dToFind = document.find('.//d')

The documentation points to more structured parsing using xpath - would encourage you to look into that.

DEMO

>>> from xml.etree import ElementTree as ET
>>> content = """<a>
...    <b>
...       <c>
...          <d>This is the text I want to retrieve</d>
...       </c>
...    </b>
... </a>
... """
>>> 
>>> xx = ET.fromstring(file) #you would be doing .parse()
>>> xx.find('d') #Returns None, as it finds only first level children
>>> xx.find('.//d')
<Element 'd' at 0xf1b550>
>>> d = xx.find('.//d')
>>> d.text
'This is the text I want to retrieve'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.