Python ElementTree支持解析未知的XML实体吗? [英] Python ElementTree support for parsing unknown XML entities?

查看:149
本文介绍了Python ElementTree支持解析未知的XML实体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组要解析的超简单XML文件...但是...它们使用自定义定义的实体.我不需要将它们映射到字符,但是我确实希望对每个字符进行解析和操作.例如:

I have a set of super simple XML files to parse... but... they use custom defined entities. I don't need to map these to characters, but I do wish to parse and act on each one. For example:

<Style name="admin-5678">
    <Rule>
      <Filter>[admin_level]='5'</Filter>
      &maxscale_zoom11;
    </Rule>
</Style>

http://effbot.org/elementtree/elementtree-xmlparser.htm 中有一个诱人的提示. XMLParser具有有限的实体支持,但是我找不到提到的方法,所有东西都会出错:

There is a tantalizing hint at http://effbot.org/elementtree/elementtree-xmlparser.htm that XMLParser has limited entity support, but I can't find the methods mentioned, everything gives errors:

    #!/usr/bin/python
    ##
    ## Where's the entity support as documented at:
    ## http://effbot.org/elementtree/elementtree-xmlparser.htm
    ## In Python 2.7.1+ ?
    ##
    from pprint     import pprint
    from xml.etree  import ElementTree
    from cStringIO  import StringIO

    parser = ElementTree.ElementTree()
   #parser.entity["maxscale_zoom11"] = unichr(160)
    testf = StringIO('<foo>&maxscale_zoom11;</foo>')
    tree = parser.parse(testf)
   #tree = parser.parse(testf,"XMLParser")
    for node in tree.iter('foo'):
        print node.text

具体取决于您调整评论的方式:

Which depending on how you adjust the comments gives:

xml.etree.ElementTree.ParseError: undefined entity: line 1, column 5

AttributeError: 'ElementTree' object has no attribute 'entity'

AttributeError: 'str' object has no attribute 'feed'           

对于那些好奇的人来说,XML来自 OpenStreetMap 的mapnik项目.

For those curious the XML is from the OpenStreetMap's mapnik project.

推荐答案

我不确定这是否是ElementTree中的错误还是什么,但是您需要在expat解析器上调用UseForeignDTD(True)来表现它的行为过去做过.

I'm not sure if this is a bug in ElementTree or what, but you need to call UseForeignDTD(True) on the expat parser to behave the way it did in the past.

这有点棘手,但是您可以通过创建自己的ElementTree.Parser实例,在xml.parsers.expat实例上调用该方法,然后将其传递给ElementTree.parse():

It's a bit hacky, but you can do this by creating your own instance of ElementTree.Parser, calling the method on it's instance of xml.parsers.expat, and then passing it to ElementTree.parse():

from xml.etree  import ElementTree
from cStringIO  import StringIO


testf = 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"

This outputs "MOOOOO"

或使用映射界面:

from xml.etree  import ElementTree
from cStringIO  import StringIO

class AllEntities:
    def __getitem__(self, key):
        #key is your entity, you can do whatever you want with it here
        return key

testf = 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"

This outputs "moo_1"

更复杂的修复方法是将ElementTree.XMLParser子类化并在那里进行修复.

A more complex fix would be to subclass ElementTree.XMLParser and fix it there.

这篇关于Python ElementTree支持解析未知的XML实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆