0

python中的etree.ElementTree包来解析我的xml文件,但它似乎没有这样做。

我的 xml 文件层次结构是这样的:root <- -> config data <> sourcefile <- -> file object1 object2 ... etc。

当我使用 print self.xml_root.findall(".\config") 时,我只得到了“[]”,这是一个空列表,谢谢

4

1 回答 1

2

如果您真的'.\config'在字符串中,那将是问题所在。这是一个字符串文字,\c用作其字符之一。即使您有'.\\config'or r'.\config',两者都指定了文字反斜杠,那仍然是错误的:

$ 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>]
于 2013-07-14T21:59:19.750 回答