public function parseTree(XMLReader $reader)
    {
        libxml_use_internal_errors(true);
        $root = new XMLNode;
        $cur = null;

        $read_anything = false;
        while ($reader->read())
        {
            $read_anything = true;
            if ($reader->nodeType === XMLReader::ELEMENT)
            {
                if ($cur === null)
                {
                    $root->name = $reader->name;
                    $cur = $root;
                }
                else
                {
                    $node = new XMLNode;
                    $node->name = $reader->name;
                    $node->parent = $cur;
                    $cur->children[] = $node;
                    $cur = $node;
                }

                if ($reader->hasAttributes)
                {
                    while ($reader->moveToNextAttribute())
                    {
                        $node = new XMLNode;
                        $node->name = $reader->name;
                        $node->content = $reader->value;
                        $cur->attributes[] = $node;
                    }
                }

            }
            else if ($reader->nodeType === XMLReader::END_ELEMENT)
            {
                $cur = $cur->parent;
            }
            else if ($reader->nodeType === XMLReader::TEXT)
            {
                $cur->content = $reader->value;
            }
        }

        try
        {
            foreach (libxml_get_errors() as $error)
                throw new XMLException($error);
        }
        finally
        {
            libxml_clear_errors();
        }

        return $root;
    }