Class Format

  • The Format class is used for outputting an XML document with customizable formatting options.

  • You can format an entire XML Document or a specific Element node into a string.

  • Formatting options can be set for encoding, indent strings, line separators, and whether to omit the XML declaration or encoding.

  • Methods like setEncoding(), setIndent(), setLineSeparator(), setOmitDeclaration(), and setOmitEncoding() allow for chaining to configure the format.

Format

A formatter for outputting an XML document, with three pre-defined formats that can be further customized.

// Log an XML document with specified formatting options.
const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>';
const document = XmlService.parse(xml);
const output = XmlService.getCompactFormat()
                   .setLineSeparator('\n')
                   .setEncoding('UTF-8')
                   .setIndent('   ')
                   .format(document);
Logger.log(output);

Methods

MethodReturn typeBrief description
format(document)StringOutputs the given Document as a formatted string.
format(element)StringOutputs the given Element node as a formatted string.
setEncoding(encoding)FormatSets the character encoding that the formatter should use.
setIndent(indent)FormatSets the string used to indent child nodes relative to their parents.
setLineSeparator(separator)FormatSets the string to insert whenever the formatter would normally insert a line break.
setOmitDeclaration(omitDeclaration)