如何像文件夹系统一样显示XML文件
我搜索了很多关于这个问题的帮助,但大多数关于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))