public function stop() {
        if (is_null($this->running)) {
            throw new xml_writer_exception('xml_writer_not_started');
        }
        if ($this->running === false) {
            throw new xml_writer_exception('xml_writer_already_stopped');
        }
        if ($this->level > 0) { // Cannot stop if not at level 0, remaining open tags
            throw new xml_writer_exception('xml_writer_open_tags_remaining');
        }
        $this->output->stop();
        $this->running = false;
    }