xml : remove a node with dom
alain walter
alain.serge.walter at gmail.com
Thu Oct 28 08:38:54 EDT 2010
Hi,
You're right for my post code : I extract it from a more complicated
context.
However, the solution you propose produce the line <aixm:elevation
uom="M"/> not a blank line.
Finally, I've found a good solution by using :
from xml.dom.ext import PrettyPrint
from xml.dom.ext.reader.Sax2 import FromXmlStream
dom = FromXmlStream("my_file")
testRemoving(dom)
PrettyPrint(dom)
def testRemoving(self,dom) :
for node1 in dom.childNodes:
if node1.nodeType == node1.ELEMENT_NODE:
for node in node1.childNodes:
if node.nodeValue == "xxx_toremove_xxx":
dom.removeChild(node1)
testRemoving(node1)
On 28 oct, 13:47, de... at web.de (Diez B. Roggisch) wrote:
> alain walter <alain.serge.wal... 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