protected Transformer createXmlTransformer ( int numberOfBlanksToIndent ) throws TransformerConfigurationException { TransformerFactoryConfigurer . Builder b = TransformerFactoryConfigurer . builder ( ) . withExternalStylesheetLoadingDisabled ( ) . withDTDLoadingDisabled ( ) ; if ( numberOfBlanksToIndent >= 0 ) { // not all TransformerFactories support this feature b = b . withSafeAttribute ( "indent-number" , numberOfBlanksToIndent ) ; } final TransformerFactory factory = b . build ( ) . configure ( TransformerFactory . newInstance ( ) ) ; final Transformer transformer = factory . newTransformer ( ) ; transformer . setOutputProperty ( OutputKeys . OMIT_XML_DECLARATION , "yes" ) ; transformer . setOutputProperty ( OutputKeys . METHOD , "xml" ) ; if ( numberOfBlanksToIndent >= 0 ) { try { transformer . setOutputProperty ( "{http://xml.apache.org/xslt}indent-amount" , String . valueOf ( numberOfBlanksToIndent ) ) ; } catch ( final IllegalArgumentException ex ) { // Could not set property '{http://xml.apache.org/xslt}indent-amount' on // transformer.getClass().getName() // which is fine for us } transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; } return transformer ; }
Create a default Transformer to format a XML - Node to a String .