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
| # -*- coding: utf-8 -*-
"""
Created on Tue Jan 31 13:30:22 2012
@author:
"""
import lxml.etree as et
import os
def makeNodes(node_dirs, root, leveldirlist, root_level):
#print 'parent', parentxml
print 'children', leveldirlist
#print 'node_dirs',node_dirs
for d in leveldirlist:
child = et.Element("node{}".format(root_level), name=d)
nodes_dirs[os.path.join(root, d)] = child
nodes_dirs[root].append(child)
if __name__ == '__main__':
# topdir = '/home/claire/Applications/ProjetPython/testxml/biblio'
# projetxml = et.Element('Project') #racine
# parent = projetxml
#
# for roots, dirs, files in os.walk(topdir):
# print roots #, '*',dirs, '*',files,'\n'
# makeNodes(parent, dirs)
topdir = '/home/claire/Applications/ProjetPython/testxml/biblio'
#topdir = '/home/claire/Applications/ImagesTest/jp'
projetxml = et.Element('Project') # racine
parent = projetxml
nodes_dirs = {topdir: parent}
ln_root = len(topdir)
for root, dirs, files in os.walk(topdir):
#print root #, '*',dirs, '*',files,'\n'
lvl = root[ln_root:].count(os.path.sep)
makeNodes(nodes_dirs, root, dirs, lvl)
print(et.tostring(projetxml,pretty_print=True)) |