public function asPrettyXML($level = 4)
    {
        // removed duplicate XML start of the document declaration
        $xml = preg_replace('/\<\?xml([^\>\/]*)\>/', '' , $this->asXML());

        $dom = new DOMDocument();
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($xml);
        $formatted = $dom->saveXml();

        return $formatted;
    }