我不确定这是否是ElementTree中的错误还是什么,但是您需要在expat解析器上调用UseForeignDTD(True)来恢复过去的行为。
这有点hacky,但是您可以通过创建自己的ElementTree.Parser实例,在xml.parsers.expat实例上调用该方法,然后将其传递给ElementTree.parse()来实现:
from xml.etree import ElementTreefrom cStringIO import StringIOtestf = StringIO('<foo>&moo_1;</foo>')parser = ElementTree.XMLParser()parser.parser.UseForeignDTD(True)parser.entity['moo_1'] = 'MOOOOO'etree = ElementTree.ElementTree()tree = etree.parse(testf, parser=parser)for node in tree.iter('foo'): print node.text
输出“ MOOOOO”
或使用映射接口:
from xml.etree import ElementTreefrom cStringIO import StringIOclass AllEntities: def __getitem__(self, key): #key is your entity, you can do whatever you want with it here return keytestf = StringIO('<foo>&moo_1;</foo>')parser = ElementTree.XMLParser()parser.parser.UseForeignDTD(True)parser.entity = AllEntities()etree = ElementTree.ElementTree()tree = etree.parse(testf, parser=parser)for node in tree.iter('foo'): print node.text
输出“ moo_1”
更复杂的修复方法是将ElementTree.XMLParser子类化并在那里进行修复。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)