aboutsummaryrefslogtreecommitdiff
blob: 410ead77538136704aa644876302648e6a1fc3f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)