Results 1 to 1 of 1

Thread: Removing children - Python - XML

  1. #1
    Join Date
    Mar 2008
    Beans
    323
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Removing children - Python - XML

    Hi, I am having some problems removing children from an xml node.

    I had:
    Code:
    node = xmldoc.getElementsByTagName('types')[0]
    for child in node.childNodes:
        print child.toxml()
        node.removeChild(child)
    That just printed a few blank lines and did nothing. Then I tried:
    Code:
    node = xmldoc.getElementsByTagName('types')[0]
    for child in node.childNodes:
        for grandchild in child.childNodes:
            print grandchild.toxml()
            child.removeChild(grandchild)
    That emptied the children but still left them there.
    eg
    Code:
    <types>
        <type>jpg</type>
        <type>png</type>
        <type>gif</type>
    </type>
    to
    Code:
    <types>
        <type/>
        <type/>
        <type/>
    </type>
    I would like to completely empty types.
    Any suggestions?


    edit: solved
    Code:
    node = xmldoc.getElementsByTagName('types')[0]
    while len(node.childNodes) > 0:
        child = node.childNodes[0]
        print child.toxml()
        node.removeChild(child)
    Last edited by MadnessRed; March 20th, 2010 at 08:36 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •