Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from suds.plugin import MessagePlugin
- from suds.sax.document import Document
- class RemoveEmptyNodes(MessagePlugin):
- """
- A Suds plugin, designed to remove empty nodes from an outgoing message before it is sent.
- """
- def marshalled(self, context):
- """
- Removes all short-closed, empty nodes from the outgoing XML's <body>.
- """
- recursive_remove(context.envelope.getChild('Body'))
- def recursive_remove(node):
- """
- Recurses down through an XML tree from ``node``, removing all empty nodes and nodes whose children are
- all empty.
- """
- for child in node.getChildren():
- if is_empty(child):
- node.remove(child)
- else:
- recursive_remove(child)
- def is_empty(node):
- """
- Using recursion, returns ``True``, if a node's entire tree contains no attributes or text values, else
- returns ``False``.
- """
- return node.isempty() or (all([is_empty(child) for child in node.getChildren()]) if
- len(node.getChildren()) > 0 else False)
Add Comment
Please, Sign In to add comment