Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. def clean_empty_nodes(xml_str):
  2. xml = fromstring(xml_str)
  3. for child in xml.iter():
  4. for key, value in child.attrib.items():
  5. if value == '':
  6. child.attrib.pop(key)
  7. if not child.attrib and not child.getchildren():
  8. child.getparent().remove(child)
  9. return etree.tostring(xml, pretty_print=True, xml_declaration=True, encoding='UTF-8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement