All pastes #969697 Raw Edit

xif

public python v1 · immutable
·published 2008-04-03 18:19 UTC
rendered paste body
from pprint import pprintfrom xml.dom import minidom, Nodedef create_tree(parent, tree, level):    for node in parent.childNodes:        # if it's a Textnode        if node.nodeType == Node.TEXT_NODE:            tree.append(node.nodeValue)        # If it's an element:        else:            tree.append(create_tree(node, [], level+1))    return treedoc = minidom.parseString('<ul><li>example.com<ul><li>Company<ul><li>History</li><li>Our people<ul><li>John</li><li>Mary</li><li>Pat</li></ul></li><li>Careers<ul><li>Current openings</li><li>Working at example.com</li></ul></li></ul></li><li>Services<ul><li>Web development</li><li>Print design</li><li>Identity and branding</li></ul></li><li>Contact us</li><li>Privacy Policy</li></ul></li></ul>')pprint(create_tree(doc.documentElement, [], 0))####~/toy/etc$ python recurse.py [[u'example.com',  [[u'Company',    [[u'History'],     [u'Our people', [[u'John'], [u'Mary'], [u'Pat']]],     [u'Careers', [[u'Current openings'], [u'Working at example.com']]]]],   [u'Services',    [[u'Web development'], [u'Print design'], [u'Identity and branding']]],   [u'Contact us'],   [u'Privacy Policy']]]]