public function parse($data, $whitespace = 1, $encoding = 'UTF-8', $reporterrors = false) {
        $data = trim($data);
        $this->xml = array();
        $this->current = array();
        $this->level = 0;
        $this->current[0] = & $this->xml;
        $parser = xml_parser_create($encoding);
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $whitespace);
        xml_set_element_handler($parser, [$this, "startelement"], [$this, "endelement"]);
        xml_set_character_data_handler($parser, [$this, "characterdata"]);
        // Start parsing an xml document.
        for ($i = 0; $i < strlen($data); $i += 4096) {
            if (!xml_parse($parser, substr($data, $i, 4096))) {
                break;
            }
        }
        if ($reporterrors) {
            $errorcode = xml_get_error_code($parser);
            if ($errorcode) {
                $exception = new xml_format_exception(xml_error_string($errorcode),
                        xml_get_current_line_number($parser),
                        xml_get_current_column_number($parser));
                xml_parser_free($parser);
                throw $exception;
            }
        }
        xml_parser_free($parser); // Deletes the parser.
        if (empty($this->xml)) { // XML file is invalid or empty, return false.
            return false;
        }
        return $this->xml;
    }