from pprint import pprint from xml.dom import minidom, Node def 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 tree doc = minidom.parseString('') 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']]]]