diff options
Diffstat (limited to 'scripts/bin/simple-xml-formatter')
-rwxr-xr-x | scripts/bin/simple-xml-formatter | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/scripts/bin/simple-xml-formatter b/scripts/bin/simple-xml-formatter new file mode 100755 index 0000000..410ead7 --- /dev/null +++ b/scripts/bin/simple-xml-formatter @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- + +''' +Make an xml file parsable for Java. +''' + +import sys + +try: + xml_file = sys.argv[1] + if xml_file == "-h" or xml_file == "--help": + raise TypeError +except: + print("Usage: simple-xml-formatter <file.xml>") + sys.exit() + +# remove strange lines that do not start with "<" and locate +# before the begining or after the ending of an xml file. +def purify_xml(lines, order = 'f'): + if order == 'r': + order = range(len(lines)) + else: + order = reversed(range(len(lines))) + + for i in order: + if lines[i].startswith('<'): + break + else: + lines[i] = '' + +with open(xml_file) as f: + lines = f.readlines() + purify_xml(lines) + purify_xml(lines, 'r') + +with open(xml_file, 'w') as f: + f.writelines(lines) |