diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php
index acd5a3bf812f4c8f1a1dcc0aaf37d1169d42a747..b978d6f6ae0de8ce1d83048398a60ab40c9e08e5 100644
--- a/lib/SimpleSAML/Metadata/SAMLBuilder.php
+++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php
@@ -53,9 +53,17 @@ class SimpleSAML_Metadata_SAMLBuilder {
 	 *
 	 * This function serializes this EntityDescriptor, and returns it as text.
 	 *
+	 * @param bool $formatted  Whether the returned EntityDescriptor should be
+	 *                         formatted first.
 	 * @return string  The serialized EntityDescriptor.
 	 */
-	public function getEntityDescriptorText() {
+	public function getEntityDescriptorText($formatted = TRUE) {
+		assert('is_bool($formatted)');
+
+		if ($formatted) {
+			SimpleSAML_Utilities::formatDOMElement($this->entityDescriptor);
+		}
+
 		return $this->document->saveXML();
 	}