public static StringWriter serializeSamlObject(final XMLObject samlObject) {
        val writer = new StringWriter();
        try {
            val marshaller = getMarshallerFactory().getMarshaller(samlObject.getElementQName());
            if (marshaller != null) {
                val element = marshaller.marshall(samlObject);
                Source domSource = new DOMSource(element);

                Result result = new StreamResult(writer);
                val tf = TransformerFactory.newInstance();
                val transformer = tf.newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
                transformer.transform(domSource, result);
            }
        } catch (final Exception e) {
            throw new SAMLException(e.getMessage(), e);
        }
        return writer;
    }
<p>serializeSamlObject.</p>

@param samlObject a {@link XMLObject} object
@return a {@link StringWriter} object
pac4j-saml/src/main/java/org/pac4j/saml/util/Configuration.java