Search code examples
pythonminidom

deleting a node with minidom


I have the following function:

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)

mydom is a minidom object When there is a match (name attribute same as name parameter to function) and the node needs to be deleted, I get the following exception:

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

It seems like it doesn't like me defining parent via node.parentNode() but that appears to be how the minidom documentation says to refer to it. I'm confused. Help?

I usually code in PHP, using Python 3.6 for some command line work with XML files.

The node with the matching name attribute should be removed from it's parent.


Solution

  • The issue was the node.parentNode() - lose the () so it is just node.parentNode and it works as expected.