Recursion: Nested dictionaries to XML(HTML)



I'm trying to figure out the logic for a bit of code I'm working on. I have a dictionary of dictionaries. Basically, I'm taking a list of files and creating a web form from it.


I'm well aware that I need to use a recursive function, but the recursion bit is throwing my mind in a million directions and I can't figure out the logic required to make this work.


My dictionary of dictionaries...



{'Desktop': {'bar': {'buz': '/home/michael/t/Desktop/bar/buz',
'fuz': '/home/michael/t/Desktop/bar/fuz'},
'foo': {'buz': '/home/michael/t/Desktop/foo/buz',
'fuz': '/home/michael/t/Desktop/foo/fuz'}},
'Documents': {'bar': {'buz': '/home/michael/t/Documents/bar/buz',
'fuz': '/home/michael/t/Documents/bar/fuz'},
'foo': {'buz': '/home/michael/t/Documents/foo/buz',
'fuz': '/home/michael/t/Documents/foo/fuz'},
'good title': '/home/michael/t/Documents/good title'},
'test.py': '/home/michael/t/test.py'}


I need that dictionary turned into this...



<ul id="master">
<li><input type="checkbox" id="Desktop"><label for="Desktop">Desktop</label>
<ul>
<li><input type="checkbox" id="Desktop/bar"><label for="Desktop/bar">bar</label>
<ul>
<li><input type="checkbox" id="/home/michael/t/Desktop/bar/buz"><label for="/home/michael/t/Desktop/bar/buz">buz</label></li>
<li><input type="checkbox" id="/home/michael/t/Desktop/bar/fuz"><label for="/home/michael/t/Desktop/bar/fuz">fuz</label></li>
<ul>
</li>
<li><input type="checkbox" id="Desktop/bar"><label for="Desktop/foo">bar</label>
<ul>
<li><input type="checkbox" id="/home/michael/t/Desktop/foo/buz"><label for="/home/michael/t/Desktop/foo/buz">buz</label></li>
<li><input type="checkbox" id="/home/michael/t/Desktop/foo/fuz"><label for="/home/michael/t/Desktop/foo/fuz">fuz</label></li>
<ul>
</li>
</ul>
</li>
<li><input type="checkbox" id="Documents"><label for="Documents">Desktop</label>
<ul>
<li><input type="checkbox" id="Documents/bar"><label for="Documents/bar">bar</label>
<ul>
<li><input type="checkbox" id="/home/michael/t/Documents/bar/buz"><label for="/home/michael/t/Documents/bar/buz">buz</label></li>
<li><input type="checkbox" id="/home/michael/t/Documents/bar/fuz"><label for="/home/michael/t/Documents/bar/fuz">fuz</label></li>
<ul>
</li>
<li><input type="checkbox" id="Documents/bar"><label for="Documents/foo">bar</label>
<ul>
<li><input type="checkbox" id="/home/michael/t/Documents/foo/buz"><label for="/home/michael/t/Documents/foo/buz">buz</label></li>
<li><input type="checkbox" id="/home/michael/t/Documents/foo/fuz"><label for="/home/michael/t/Documents/foo/fuz">fuz</label></li>
<ul>
</li>
<li><input type="checkbox" id="/home/michael/t/Documents/good title"><label for="/home/michael/t/Documents/good title">good title</label></li>
</ul>
</li>
<li><input type="checkbox" id="/home/michael/t/test.py"><label for="/home/michael/t/test.py">test.py</label></li>
</ul>


Recursion is definitely a puzzle to me and this one is (to me) a rather advanced puzzle.


No comments:

Post a Comment