xml : remove a node with dom

Diez B. Roggisch deets at web.de
Thu Oct 28 13:47:21 CEST 2010


alain walter <alain.serge.walter at gmail.com> writes:

> Hello,
> I have many difficulties to manipulate xml routines. I'm working with
> python 2.4.4 and I cannot change to a more recent one, then I use dom
> package, why not.
> In the following code, I'm trying unsuccessfully to remove a
> particular node. It seems to me that it should be basic, but it's
> not.
> Thanks for your help

If you post code, make an attempt for it to be runnable. I had to fix
numerous simple errors to make it run & actually solve your problem.

from xml.dom.minidom import parse,parseString
from xml.dom import Node


toxml="""
<aixm:VORTimeSlice xmlns:aixm="aixm" xmlns:gml="gml" gml:id="ABB">
   <aixm:type>ABB</aixm:type>
      <aixm:designator>ABB</aixm:designator>
      <aixm:ElevatedPoint gml:id="ABB" srsDimension="2">
         <gml:pos srsDimension="2">-51.23 4.6501</gml:pos>
         <aixm:elevation uom="M">xxx_toremove_xxx</aixm:elevation>
      </aixm:ElevatedPoint>
</aixm:VORTimeSlice>"""


dom = parseString(toxml)

def ApplicationWhitespaceRemoving(ele) :
   for c in ele.childNodes:
      if c.nodeType == c.TEXT_NODE:
         if c.nodeValue == "xxx_toremove_xxx":
             parent = c.parentNode
             parent.removeChild(c)
      elif c.nodeType == ele.ELEMENT_NODE:
         ApplicationWhitespaceRemoving(c)


ApplicationWhitespaceRemoving(dom)

print dom.toxml()

--

Diez



More information about the Python-list mailing list