xml : remove a node with dom

alain walter

Usenet Archive
Joined
Oct 28, 2010
Messages
2
Reaction score
0
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="<aixm:VORTimeSlice 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>"

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)
 

Diez B. Roggisch

Usenet Archive
Joined
Jul 27, 2003
Messages
4,325
Reaction score
0
alain walter said:
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
 

Stefan Behnel

Usenet Archive
Joined
Sep 30, 2004
Messages
1,624
Reaction score
0
alain walter, 28.10.2010 11:37:
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)

Just a comment on code style here. Functions and procedures should have names that strongly refer to a descriptive verb, consequently starting with a lower case letter by convention. So a better name for your procedure above could be "removeApplicationSpecificWhitespace".

When using constants defined on a module or class, try to refer to them in a consistent way, either by importing the names globally into your module namespace, or by referencing them from the same namespace (e.g. class object) everywhere. Simple, recurrent patterns help in reading your code.

Then, I don't see what the "Node" import is used for, and it looks like your function is actually a method (called as self.xxx). When presenting code snippets, try to make them consistent, or make it clear that (and where) things are missing.

Hope that helps,

Stefan
 

alain walter

Usenet Archive
Joined
Oct 28, 2010
Messages
2
Reaction score
0
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top