Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ElementTree find not working

I am new to ElementTree. I'm trying to snag the <sid> value from an XML response.

The following code is not working for me. How do I extract the value in <sid> ? I am not sure where the number 53 is coming from here.

    ...
    r = requests.post(self.dispatchurl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))
    print r.content
    tree = ET.ElementTree(r.content)
    print tree.find('sid')

output:

/usr/bin/python2.7 /home/myuser/PycharmProjects/autoshun/shunlibs/SplunkSearch.py
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <sid>super__awesome__search__searchname_at_1489433276_24700</sid>
</response>

53

Process finished with exit code 0
like image 843
dobbs Avatar asked Oct 15 '25 04:10

dobbs


1 Answers

I know the OP is almost 2 years old, however I came across the same issue and found the solution that worked for me. What worked was removing the namespace right after the parse call which loads the XML file.

A function like this:

def remove_namespace(xmldoc, namespace):
    ns = u'{%s}' % namespace
    nsl = len(ns)
    for elem in xmldoc.getiterator():
        if elem.tag.startswith(ns):
            elem.tag = elem.tag[nsl:]

I have discovered that ElementTree has issues with namespaces, you either strip them out using a function like the one above, or you have to pass the explicit namespace to the find or findall functions.

like image 170
Dominik Avatar answered Oct 17 '25 16:10

Dominik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!