Sciguy77
Sciguy77

Reputation: 349

ElementTree find returns None?

I'm using ElementTree with Python to parse an XML file to find the contents of the tag contentType.

Here's the Python line:

extensionType = ET.parse("src/" + str(filename)).find('contentType')

And here's the XML:

<?xml version="1.0" encoding="UTF-8"?>
<StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
    <cacheControl>Private</cacheControl>
    <contentType>image/jpeg</contentType>
</StaticResource>

What am I doing wrong?

Thanks!

Upvotes: 0

Views: 3016

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46849

you are just parsing the xml file so far. this is how you can get your element using xpath (note how you have to use the given xml namespace xmlns)

import xml.etree.cElementTree as ET

tree = ET.parse('test.xml')

root = tree.getroot()

xmlns = {'soap': '{http://soap.sforce.com/2006/04/metadata}'}
ct_element = root.find('.//{soap}contentType'.format(**xmlns))
print(ct_element.text)

Upvotes: 2

Related Questions