Interface DomTripVisitor


public interface DomTripVisitor
Visitor interface for structured depth-first tree traversal with enter/exit lifecycle callbacks.

This complements the existing stream-based navigation (Element.descendants(), ElementQuery) with a pattern better suited for transformations needing context tracking (depth, ancestors, accumulated state).

Traversal Order:

The visitor performs a depth-first traversal: for each element, enterElement(Element) is called first, then all children are visited recursively, and finally exitElement(Element) is called.

Flow Control:

Visit methods return an DomTripVisitor.Action to control traversal:

Usage Example:

element.accept(new DomTripVisitor() {
    @Override
    public Action enterElement(Element e) {
        if ("metadata".equals(e.localName())) {
            return Action.SKIP; // don't descend into metadata
        }
        System.out.println("Entering: " + e.name());
        return Action.CONTINUE;
    }

    @Override
    public void exitElement(Element e) {
        System.out.println("Exiting: " + e.name());
    }

    @Override
    public Action visitComment(Comment c) {
        System.out.println("Comment: " + c.content());
        return Action.CONTINUE;
    }
});

Stateful Visitor Example:

class NamespaceCollector implements DomTripVisitor {
    private final Map<String, String> namespaces = new LinkedHashMap<>();
    private final Deque<String> path = new ArrayDeque<>();

    @Override
    public Action enterElement(Element e) {
        path.push(e.localName());
        String ns = e.namespaceURI();
        if (ns != null && !namespaces.containsKey(ns)) {
            namespaces.put(ns, String.join("/", path));
        }
        return Action.CONTINUE;
    }

    @Override
    public void exitElement(Element e) {
        path.pop();
    }

    public Map<String, String> result() { return namespaces; }
}
Since:
1.3.0
See Also: