相关疑难解决方法(0)

Python ElementTree支持解析未知的XML实体?

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

<Style name="admin-5678">
    <Rule>
      <Filter>[admin_level]='5'</Filter>
      &maxscale_zoom11;
    </Rule>
</Style>
Run Code Online (Sandbox Code Playgroud)

http://effbot.org/elementtree/elementtree-xmlparser.htm上有一个诱人的暗示,XMLParser对实体的支持有限,但我找不到提到的方法,一切都会出错:

    #!/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
Run Code Online (Sandbox Code Playgroud)

这取决于您如何调整评论:

xml.etree.ElementTree.ParseError: undefined entity: line 1, column 5
Run Code Online (Sandbox Code Playgroud)

要么

AttributeError: 'ElementTree' object has no attribute 'entity'
Run Code Online (Sandbox Code Playgroud)

要么

AttributeError: 'str' …
Run Code Online (Sandbox Code Playgroud)

python xml openstreetmap

20
推荐指数
2
解决办法
2万
查看次数

标签 统计

openstreetmap ×1

python ×1

xml ×1