xml : remove a node with dom

2010-10-28 Thread alain walter
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

toxml="
   ABB
  ABB
  
 -51.23 4.6501
 xxx_toremove_xxx
  
"

from xml.dom.minidom import parse,parseString

dom = parseString(toxml)
self.ApplicationWhitespaceRemoving(dom)
print toxml

def ApplicationWhitespaceRemoving(self,ele) :
   from xml.dom import Node
   for c in ele.childNodes:
  if c.nodeType == c.TEXT_NODE:
 if c.nodeValue == "xxx_toremove_xxx":
???.removeChild(???)
  elif c.nodeType == ele.ELEMENT_NODE:
 self.ApplicationWhitespaceRemoving(c)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml : remove a node with dom

2010-10-28 Thread alain walter
Hi,
You're right for my post code : I extract it from a more complicated
context.
However, the solution you propose produce the line  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...@web.de (Diez B. Roggisch) wrote:
> alain walter  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="""
> 
>    ABB
>       ABB
>       
>          -51.23 4.6501
>          xxx_toremove_xxx
>       
> """
>
> 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

-- 
http://mail.python.org/mailman/listinfo/python-list