Alter namespace prefixing with ElementTree in Python

前端 未结 2 1248
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 03:18

By default, when you call ElementTree.parse(someXMLfile) the Python ElementTree library prefixes every parsed node with it\'s namespace URI in Clark\'s Notation:

         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 03:19

    xml.etree.ElementTree doesn't appear to have fixtag, well, not according to the documentation. However I've looked at some source code for fixtag and you do:

    import xml.etree.ElementTree as ET
    
    for event, elem in ET.iterparse(inFile, events=("start", "end")):
        namespace, looktag = string.split(elem.tag[1:], "}", 1)
    

    You have the tag string in looktag, suitable for a lookup. The namespace is in namespace.

提交回复
热议问题