ptree

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):

  • ptree.ParentedTree: A drop-in replacement for ElementTree where each element keeps track of a single parent pointer, returned by the element's parent() method. Elements may have at most one parent; i.e., subtrees may not be shared. Any attempt to do use a single element as a child for multiple parents will generate a ValueError.
  • ptree.MultiParentedTree: A drop-in replacement for ElementTree where each element keeps track of a list of parent pointers, returned by the element's parents() method. Elements may have zero or more parients; i.e., subtrees may be shraed. If a single Element is used as multiple children of the same parent, then that parent will appear multiple times in the parents list.

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.

Example Usage

# 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])

More Information