Guest User

Untitled

a guest
Jan 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. from suds.plugin import MessagePlugin
  2. from suds.sax.document import Document
  3.  
  4.  
  5. class RemoveEmptyNodes(MessagePlugin):
  6. """
  7. A Suds plugin, designed to remove empty nodes from an outgoing message before it is sent.
  8. """
  9. def marshalled(self, context):
  10. """
  11. Removes all short-closed, empty nodes from the outgoing XML's <body>.
  12. """
  13. recursive_remove(context.envelope.getChild('Body'))
  14.  
  15.  
  16. def recursive_remove(node):
  17. """
  18. Recurses down through an XML tree from ``node``, removing all empty nodes and nodes whose children are
  19. all empty.
  20. """
  21. for child in node.getChildren():
  22. if is_empty(child):
  23. node.remove(child)
  24. else:
  25. recursive_remove(child)
  26.  
  27.  
  28. def is_empty(node):
  29. """
  30. Using recursion, returns ``True``, if a node's entire tree contains no attributes or text values, else
  31. returns ``False``.
  32. """
  33. return node.isempty() or (all([is_empty(child) for child in node.getChildren()]) if
  34. len(node.getChildren()) > 0 else False)
Add Comment
Please, Sign In to add comment