Skip to content

Commit 8f22a84

Browse files
author
p12
committed
Transform: use lxml.etree to create file mapping XML file
1 parent 10917e4 commit 8f22a84

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

build_link_map.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# filename -> title mapping to a xml file.
2222

2323
import fnmatch
24+
import lxml.etree as e
2425
import re
2526
import os
2627

@@ -31,8 +32,7 @@
3132
html_files.append(os.path.join(root, filename))
3233

3334
# create an xml file containing mapping between page title and actual location
34-
out = open('link-map.xml', 'w')
35-
out.write('<?xml version="1.0" encoding="UTF-8"?><files>\n')
35+
root = e.Element('files')
3636

3737
for fn in html_files:
3838
f = open(fn, "r")
@@ -51,7 +51,11 @@
5151
title = m.group(1)
5252

5353
target = os.path.relpath(os.path.abspath(fn), os.path.abspath('output'))
54-
out.write(' <file from="' + title + '" to="' + target + '" />\n')
54+
file_el = e.SubElement(root, 'file')
55+
file_el.set('from', title)
56+
file_el.set('to', target)
5557

56-
out.write('</files>')
58+
out = open('link-map.xml', 'w')
59+
out.write('<?xml version="1.0" encoding="UTF-8"?>')
60+
out.write(e.tostring(root, pretty_print=True))
5761
out.close()

0 commit comments

Comments
 (0)