Coderzelf
Coderzelf

Reputation: 752

XML parser python fails to get element

etree.ElementTree package in python to parse my xml file but it seems it fails to do so.

My xml file hierarchy is like this: root <- -> config data <> sourcefile <- -> file object1 object2 ... etc.

when I use print self.xml_root.findall(".\config"), I only got "[]", which is an empty list, thanks

Upvotes: 0

Views: 147

Answers (1)

torek
torek

Reputation: 488183

If you really have '.\config' in the string, that would be the problem. That's a string literal using \c as one of its characters. Even if you have '.\\config' or r'.\config', both of which specify a literal backslash, that would still be wrong:

$ cat eleme.py
import xml.etree.ElementTree as ET

root = ET.fromstring("""
<root>
  <config>
    source
  </config>
  <config>
    source
  </config>
</root>""")

print r'using .\config', root.findall('.\config')
print r'using .\\config', root.findall('.\\config')
print 'using ./config', root.findall('./config')
$ python2.7 eleme.py 
using .\config []
using .\\config []
using ./config [<Element 'config' at 0x8017a8610>, <Element 'config' at 0x8017a8650>]

Upvotes: 2

Related Questions