private function attachHandlers($parser)
    {
        $onElementStart = \Closure::bind(function ($parser, $name, $attributes) {
            $name                 = $this->normalize($name);
            $this->currentElement = $name;
            $this->dataBuffer     = null;

            $this->stackSize++;

            $this->onElementStart($parser, $name, $attributes);
        }, $this);

        $onElementEnd   = \Closure::bind(function ($parser, $name) {
            $name                 = $this->normalize($name);
            $this->currentElement = null;

            $this->stackSize--;

            if (null !== $this->dataBuffer) {
                $this->onElementData($parser, $this->dataBuffer);
            }

            $this->dataBuffer = null;

            $this->onElementEnd($parser, $name);
        }, $this);

        $onElementData  =  \Closure::bind(function ($parser, $data) {
            $this->dataBuffer .= $data;
        }, $this);

        $onNamespaceDeclarationStart = \Closure::bind(function ($parser, $prefix, $uri) {
            $this->namespaces[$prefix] = rtrim($uri, '/');
            $this->onNamespaceDeclarationStart($parser, $prefix, $uri);
        }, $this);

        $onNamespaceDeclarationEnd = \Closure::bind(function ($parser, $prefix) {
            $this->onNamespaceDeclarationEnd($parser, $prefix);
        }, $this);

        xml_set_element_handler($parser, $onElementStart, $onElementEnd);

        xml_set_character_data_handler($parser, $onElementData);

        xml_set_start_namespace_decl_handler($parser, $onNamespaceDeclarationStart);

        xml_set_end_namespace_decl_handler($parser, $onNamespaceDeclarationEnd);

        return $this;
    }