Document Tree Traversal#
Overview#
DoclingDocument.iterate_items() is the primary API for depth-first traversal of the document element tree. It yields (NodeItem, int) tuples — the node and its nesting level — and accepts filters for content layers, item types, and page scope.
Source: iterate_items() definition in docling_core/types/doc/document.py. The public method delegates all heavy lifting to _iterate_items_with_stack().
Method Signature#
def iterate_items(
self,
root: Optional[NodeItem] = None,
with_groups: bool = False,
traverse_pictures: bool = False,
page_no: Optional[int] = None,
included_content_layers: Optional[set[ContentLayer]] = None,
_level: int = 0, # deprecated
) -> Iterable[tuple[NodeItem, int]]:
Parameters#
| Parameter | Default | Effect |
|---|---|---|
root | None | Start node; defaults to document.body |
with_groups | False | If True, GroupItem container nodes are also yielded |
traverse_pictures | False | If True, children of PictureItem nodes (e.g. OCR captions) are traversed |
page_no | None | Restrict output to items whose provenance falls on this page |
included_content_layers | None | Set of ContentLayer values to include; None → DEFAULT_CONTENT_LAYERS = {ContentLayer.BODY} |
_level | 0 | Deprecated. The returned int level is derived from stack depth, not this parameter. |
How Traversal Works#
_iterate_items_with_stack() performs an iterative depth-first walk starting at root (defaults to self.body) . For each node, it evaluates three conditions before yielding :
- Type gate — skip
GroupItemunlesswith_groups=True - Page gate — skip
DocItemnodes whoseprovlist doesn't containpage_no(when set) - Layer gate — skip nodes whose
content_layeris not inincluded_content_layers(orDEFAULT_CONTENT_LAYERS)
Children are resolved via RefItem.resolve(doc) and recursed . When traverse_pictures=False (the default), children of a PictureItem that are not captions are skipped .
The yielded int level equals len(stack) at the point of yield — the stack tracks child indices, not depth explicitly .
Content Layers#
ContentLayer controls which functional layer an item belongs to :
| Value | Meaning |
|---|---|
BODY | Main document content (default) |
FURNITURE | Page headers and footers |
BACKGROUND | Watermarks and background elements |
INVISIBLE | Hidden text |
NOTES | Speaker/author notes |
DEFAULT_CONTENT_LAYERS = {ContentLayer.BODY} means furniture, notes, and other layers are silently excluded unless explicitly requested. To include them:
from docling_core.types.doc import ContentLayer
# Include headers and footers in iteration
for item, level in doc.iterate_items(
included_content_layers={ContentLayer.BODY, ContentLayer.FURNITURE}
):
...
To get everything: included_content_layers=set(ContentLayer) — this is the pattern used by print_element_tree() and export_to_element_tree().
Key Usage Patterns#
Basic iteration (leaf content nodes only):
for item, level in doc.iterate_items():
...
Including group container nodes:
for item, level in doc.iterate_items(with_groups=True):
if isinstance(item, GroupItem):
...
OCR text in scanned PDFs — OCR text is stored as children of PictureItem; you must opt in with traverse_pictures=True :
for item, level in doc.iterate_items(traverse_pictures=True):
...
Single-page extraction:
for item, level in doc.iterate_items(page_no=3):
...
Custom root — traverse a subtree rooted at any NodeItem:
for item, level in doc.iterate_items(root=some_section_node):
...
Downstream Consumers#
iterate_items() is the backbone for all export methods. They all accept the same filtering parameters and thread them through to the traversal:
export_to_markdown()— via internal callsexport_to_html(),export_to_text()— same patternprint_element_tree()/export_to_element_tree()— usewith_groups=True, traverse_pictures=True, included_content_layers=set(ContentLayer)to dump the full tree
Key Source References#
| Artifact | Location |
|---|---|
iterate_items() public API | docling_core/types/doc/document.py |
_iterate_items_with_stack() implementation | docling_core/types/doc/document.py |
ContentLayer enum + DEFAULT_CONTENT_LAYERS | docling_core/types/doc/document.py |
NodeItem base class | docling_core/types/doc/document.py (L1706–1801) |
Test: test_reference_doc() | test/test_docling_doc.py |
Test: test_export_traverse_pictures_ocr_scanned_pdf() | test/test_docling_doc.py |