protected function marshall(QtiComponent $component)
    {
        // Reset.
        $this->resetTrail();
        $this->resetFinal();
        $this->resetMark();
        $this->resetProcessed();

        $this->pushTrail($component);

        while ($this->countTrail() > 0) {

            $node = $this->popTrail();

            if (!$node instanceof DOMElement && !$this->isMarked($node) && !$this->isComponentFinal($node)) {
                // Hierarchical node, 1st pass.
                $this->mark($node);
                $this->pushTrail($node); // repush for a further pass.
                $children = array_reverse($this->getChildrenComponents($node)); // next nodes to explore.

                foreach ($children as $c) {
                    $this->pushTrail($c);
                }
            } elseif ($this->isMarked($node)) {
                // Push the result on the trail.
                $finals = $this->emptyFinal(count($this->getChildrenComponents($node)));
                $marshaller = $this->getMarshallerFactory()->createMarshaller($node);
                $element = $marshaller->marshallChildrenKnown($node, $finals);
                $this->pushProcessed($element);

                if ($node === $component) {
                    // This is our second pass on the root element, the process is finished.
                    return $element;
                } else {
                    $this->pushTrail($element);
                }
            } else {
                // It's a leaf!
                if ($node instanceof DOMElement) {
                    $this->pushFinal($node);
                } else {
                    $marshaller = $this->getMarshallerFactory()->createMarshaller($node);
                    $processed = $marshaller->marshall($node);
                    $this->pushFinal($processed);
                    $this->pushProcessed($processed);
                }
            }
        }
    }