public function format($xml)
    {
        $dom = new DOMDocument('1.0');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($xml);
        $formatted = $dom->saveXML();

        if (!$formatted) {
            throw new Exception("An error occurred while formatting the XML");
        }

        return $formatted;
    }