Skip to content

删除remove topic的方法 #19

@xiongyifan

Description

@xiongyifan

removeChild方法直接调用会报错, 相关Issue, xmindltd/xmind-sdk-python#2

调用下面的remove_node方法, 然后再save, topic就会被删除

例如: 小明-小红, 想要删除小红, 调用remove_node(小红)

def remove_topic(topic_remove, topic_parent=None):
    """
    删除Topic
    Args:
        topic_remove: 想要删除的topic
        topic_remove: 想要删除的topic的父topic, 使用方法1时会用到, 方法2不需要
        
    """
    # 方法1
    # 当直接调用ElementTopic中的removeChild方法, 实际调用的是父类Element的removeChild
    #
    # def removeChild(self, child_node):
    #     child_node = child_node.getImplementation()
    #     self._node.removeChild(child_node)  # 这里的self._node是xml.dom.minidom.Node类型
    #
    # 所以关键方法就是xml.dom.minidom.Node中的removeChild
    # def removeChild(self, oldChild):
    #     try:
    #         self.childNodes.remove(oldChild)  # self.childNodes是list, 所以核心就是list删除一个元素
    #     except ValueError:
    #         raise xml.dom.NotFoundErr()
    #
    # 所以需要找父节点的childNodes, 和子节点中的node, 只要子节点的node在父节点childNodes中, 删除即可
    #
    # 找父节点的childNodes, 下面的例子是, 一个父节点带一个子节点
    # topic_parent._node, <DOM Element: topic at 0x7f85eb904310>
    # topic_parent._node.childNodes, [<DOM Element: title at 0x7f85eb9043a0>,
    #                                   <DOM Element: children at 0x7f85eb904430>]
    # topic_parent._node.childNodes[1], <DOM Element: children at 0x7f85eb904430>
    # topic_parent._node.childNodes[1].childNodes, [<DOM Element: topics at 0x7f85eb9044c0>]
    # topic_parent._node.childNodes[1].childNodes[0], <DOM Element: topics at 0x7f85eb9044c0>
    # topic_holding._node.childNodes[1].childNodes[0].childNodes, [<DOM Element: topic at 0x7f85eb904550>]
    #
    # 找子节点的node
    # topic_remove._node, <DOM Element: topic at 0x7f85eb904550>
    #
    # 然后发现topic_remove._node的id和topic_holding._node.childNodes[1].childNodes[0].childNodes[0]的id是一样的
    # 调用topic_parent._node.childNodes[1].childNodes[0].removeChild(topic_remove._node), 就从父节点中删除了子节点
    # 方法1的最终结果:
    # topic_parent._node.childNodes[1].childNodes[0].removeChild(topic_remove._node)

    # 方法2: 更简单
    topic_remove.getParentNode().removeChild(topic_remove._node)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions