Warning: This package is fairly new, and should be considered alpha (testing) software.
ptree is a Python package that extends the xml.etree package (which became part of Python's standard library with Python 2.5) to support trees with parent pointers. Parent pointers are handled automatically -- e.g., when you insert an element into another element, its parent pointer will be set automatically. The ptree package defines two objects that provide the same interface as the xml.etree.ElementTree module (and the xml.etree.cElementTree module):
Aside from the parent() and parents() methods, these two drop-in replacements act exactly like the original ElementTree module. See the xml.etree documentation for information about how to use them.
# Import the single-parented version of ElementTree. import ptree.ParentedTree as PT # Create a simple tree. tree = PT.fromstring("""\ <html> <head><title>Page Title</title></head> <body bgcolor="#ffffff">Hello world!</body> </html>""") # Get the body element. body = tree[1] # Navigate from the body to the title. (NEW) title = PT.tostring(body.parent()[0][0])