Last active
July 28, 2018 01:03
-
-
Save ruffsl/251d10ec2a815e0d6c9c5a8002ba3036 to your computer and use it in GitHub Desktop.
Using XML schema to find and enable substitution of missing elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ElementTree | |
from xml.dom import minidom | |
def tidy_xml(element): | |
subiter = ElementTree.ElementTree(element).getiterator() | |
for x in subiter: | |
if len(x): | |
if x.text: | |
x.text = x.text.strip() | |
if x.tail: | |
x.tail = x.tail.strip() | |
return element | |
def pretty_xml(element): | |
xmlstr = ElementTree.tostring(element, encoding='unicode', method='xml') | |
xmlstr = minidom.parseString(xmlstr).toprettyxml(indent=' ', newl='\n', encoding='utf-8') | |
return xmlstr.decode('utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment