Hi I found something like a bug, I can't get this working:
import xml.dom.minidom as minidom
document="""<a>
<b>
<c />
<c />
</b>
</a>"""
dom = minidom.parseString(document)
dom.childNodes
dom.childNodes[0].childNodes
dom.childNodes[0].childNodes[1].childNodes
def delete_recursive(parent):
for child in parent.childNodes:
if child.hasChildNodes():
delete_recursive(child)
else:
parent.removeChild(child)
delete_recursive(dom)
dom.childNodes
dom.childNodes[0].childNodes
dom.childNodes[0].childNodes[0].childNodes
Executes as:
>>> import xml.dom.minidom as minidom
>>> document="""<a>
... <b>
... <c />
... <c />
... </b>
... </a>"""
>>> dom = minidom.parseString(document)
>>>
>>> dom.childNodes
[<DOM Element: a at 0x7f5408e717d0>]
>>> dom.childNodes[0].childNodes
[<DOM Text node "'\n '">, <DOM Element: b at 0x7f5408e71f50>, <DOM Text node "'\n'">]
>>> dom.childNodes[0].childNodes[1].childNodes
[<DOM Text node "'\n '">, <DOM Element: c at 0x7f5408e93290>, <DOM Text node "'\n '">, <DOM Element: c at 0x7f5408e93310>, <DOM Text node "'\n '">]
>>>
>>> def delete_recursive(parent):
... for child in parent.childNodes:
... if child.hasChildNodes():
... delete_recursive(child)
... else:
... parent.removeChild(child)
...
>>> delete_recursive(dom)
>>>
>>> dom.childNodes
[<DOM Element: a at 0x7f5408e717d0>]
>>> dom.childNodes[0].childNodes
[<DOM Element: b at 0x7f5408e71f50>]
>>> dom.childNodes[0].childNodes[0].childNodes
[<DOM Text node "'\n '">, <DOM Element: c at 0x7f5408e93290>, <DOM Text node "'\n '">, <DOM Element: c at 0x7f5408e93310>, <DOM Text node "'\n '">]
The problem here is this last line, those text nodes shouldn't be here! I have traced the problem, and seem that the for loop is not correctly executed
|