code: public static String formatXMLContent(String content) throws TransformerFactoryConfigurationError,
          TransformerException
      {
          Transformer transformer = TransformerFactory.newInstance().newTransformer();
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  
          StreamResult result = new StreamResult(new StringWriter());
  
          // Use a SAX Source instead of a StreamSource so that we can control the XMLReader used and set up one that
          // doesn't resolve entities (and thus doesn't go out on the internet to fetch DTDs!).
          SAXSource source = new SAXSource(new InputSource(new StringReader(content)));
          try {
              XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
              reader.setEntityResolver(new org.xml.sax.EntityResolver() {
                  @Override
                  public InputSource resolveEntity(String publicId, String systemId)
                      throws SAXException, IOException
                  {
                      // Return an empty resolved entity. Note that we don't return null since this would tell the reader
                      // to go on the internet to fetch the DTD.
                      return new InputSource(new StringReader(""));
                  }
              });
              source.setXMLReader(reader);
          } catch (Exception e) {
              throw new TransformerException(String.format(
                  "Failed to create XML Reader while pretty-printing content [%s]", content), e);
          }
  
          transformer.transform(source, result);
  
          return result.getWriter().toString();
      }
docs: Parse and pretty print a XML content.
  
  @param content the XML content to format
  @return the formated version of the passed XML content
  @throws TransformerFactoryConfigurationError when failing to create a
  {@link TransformerFactoryConfigurationError}
  @throws TransformerException when failing to transform the content
  @since 5.2M1