【发布时间】:2010-12-16 22:25:59
【问题描述】:
我已经搜索了很多关于这方面的帮助,但是关于 XML 和 Python 的大多数信息都是关于如何读取和解析文件,而不是如何编写它们。我目前有一个将文件夹的文件系统输出为 XML 文件的脚本,但我希望该文件包含可以在网络上读取的 HTML 标记。
另外,我正在寻找 XML 信息的图形文件夹样式显示。我已经看到用 javascript 解决了这个问题,但我没有任何 javascript 经验,所以这在 python 中可能吗?
import os
import sys
from xml.sax.saxutils import quoteattr as xml_quoteattr
dirname = sys.argv[1]
a = open("output2.xml", "w")
def DirAsLessXML(path):
result = '<dir name = %s>\n' % xml_quoteattr(os.path.basename(path))
for item in os.listdir(path):
itempath = os.path.join(path, item)
if os.path.isdir(itempath):
result += '\n'.join(' ' + line for line in
DirAsLessXML(os.path.join(path, item)).split('\n'))
elif os.path.isfile(itempath):
result += ' <file name=%s />\n' % xml_quoteattr(item)
result += '</dir>'
return result
if __name__ == '__main__':
a.write('<structure>\n' + DirAsLessXML(dirname))
【问题讨论】:
-
如果它需要在 Web 浏览器中运行,则必须使用 JavaScript。