forked from kamekame/alpha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_to_html.py
executable file
·42 lines (35 loc) · 1.11 KB
/
xml_to_html.py
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
39
40
41
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Create a html-page from bookmarks of a xml-files.
Sort files dependent of the category.
An '&' in an url doesn't work because of the xml-file.
"""
import webbrowser
from lxml import etree
# get content from xml
xmlpath = "/home/kame/Dropbox/data/links.xml"
xml = etree.parse(xmlpath)
# print(etree.tostring(xml))
# todo - sort for category
# todo - check for empty xml-items
# convert xml data to html structure and save data in html-file
url = "/tmp/links.html"
f = open(url, "w")
f.write("<html>")
f.write("<head>")
f.write("</head>")
f.write("<body>")
f.write("My bookmarks:<br>")
n_items = len(xml.xpath("//item"))
print(n_items)
for i in range(1, n_items + 1):
f.write(str(xml.xpath("//item[" + str(i) + "]/category/text()")[0]))
f.write(str("\t\t\t"))
name_ = str(xml.xpath("//item[" + str(i) + "]/name/text()")[0])
f.write("<a href=\"" + str(xml.xpath("//item[" + str(i) + "]/url/text()")[0]) + "\">" + str(name_) + "</a><br>")
f.write("</body>")
f.write("</html>")
# open html-file
new = 2 # open in a new tab, if possible
webbrowser.open(url, new=new)