删除具有最小约束的节点

问题描述 投票:1回答:1

我具有以下功能:

def removeNodes(mydom, name):
    nodeList = mydom.getElementsByTagName('option')
    # in php removing node has to be done in reverse order or it changes the
    #  nodeList. I assume same is true with python minidom
    for node in reversed(nodeList):
        if node.hasAttribute('name') and node.getAttribute('name') == name:
            parent = node.parentNode()
            parent.removeChild(node)

神话是一个极小的对象当存在匹配项(名称属性与要使用的名称参数相同)并且需要删除节点时,出现以下异常:

Traceback (most recent call last):
  File "./iBooksOptions.py", line 163, in <module>
    main() 
  File "./iBooksOptions.py", line 160, in main
    modifyMetaFile(xmlpath)
  File "./iBooksOptions.py", line 79, in modifyMetaFile
    removeNodes(mydom, 'specified-fonts')
  File "./iBooksOptions.py", line 14, in removeNodes
    parent = node.parentNode()
TypeError: 'Element' object is not callable

似乎不喜欢我通过node.parentNode()定义父级,但这似乎是minidom文档说的引用它的方式。我糊涂了。帮助?

我通常使用PHP 3.6编写代码,使用Python 3.6处理XML文件的某些命令行。

具有匹配名称属性的节点应从其父节点移除。

python minidom
1个回答
0
投票

问题是node.parentNode()-失去了(),所以它只是node.parentNode,并且可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.