xml : remove a node with dom

Stefan Behnel stefan_ml at behnel.de
Thu Oct 28 08:17:58 EDT 2010


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




More information about the Python-list mailing list