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 likesetEncoding(),setIndent(),setLineSeparator(),setOmitDeclaration(), andsetOmitEncoding() allow for chaining to configure the format.

Format

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

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

Methods

MethodReturn typeBrief description
format(document)StringOutputs the givenDocument as a formatted string.
format(element)StringOutputs the givenElement 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)FormatSets whether the formatter should omit the XML declaration, such as<?xml version="1.0"encoding="UTF-8"?>.
setOmitEncoding(omitEncoding)FormatSets whether the formatter should omit the encoding in the XML declaration, such as theencoding field in<?xml version="1.0" encoding="UTF-8"?>.

Detailed documentation

format(document)

Outputs the givenDocument as a formatted string.

Parameters

NameTypeDescription
documentDocumentthe document to format

Return

String — the formatted document


format(element)

Outputs the givenElement node as a formatted string.

Parameters

NameTypeDescription
elementElementthe element to format

Return

String — the formatted element


setEncoding(encoding)

Sets the character encoding that the formatter should use. Theencoding argument mustbe an accepted XML encoding likeISO-8859-1,US-ASCII,UTF-8, orUTF-16.

// Log an XML document with encoding that does not support certain special// characters.constxml='<root><a><b>ಠ‿ಠ</b><b>ಠ‿ಠ</b></a></root>';constdocument=XmlService.parse(xml);constoutput=XmlService.getRawFormat().setEncoding('ISO-8859-1').format(document);Logger.log(output);

Parameters

NameTypeDescription
encodingStringthe encoding to use

Return

Format — the formatter, for chaining


setIndent(indent)

Sets the string used to indent child nodes relative to their parents. Setting an indent otherthannull will cause the formatter to insert a line break after every node.

// Log an XML document with each child node indented four spaces.constxml='<root><a><b>Text!</b><b>More text!</b></a></root>';constdocument=XmlService.parse(xml);constoutput=XmlService.getCompactFormat().setIndent('    ').format(document);Logger.log(output);

Parameters

NameTypeDescription
indentStringthe indent to use

Return

Format — the formatter, for chaining


setLineSeparator(separator)

Sets the string to insert whenever the formatter would normally insert a line break. The threepre-defined formatters have different conditions under which they insert a line break. Thedefault line separator is\r\n.

// Log an XML document with several spaces and a pipe character in place of line// breaks.constxml='<root><a><b>Text!</b><b>More text!</b></a></root>';constdocument=XmlService.parse(xml);constoutput=XmlService.getRawFormat().setLineSeparator(' | ').format(document);Logger.log(output);

Parameters

NameTypeDescription
separatorStringthe separator to use

Return

Format — the formatter, for chaining


setOmitDeclaration(omitDeclaration)

Sets whether the formatter should omit the XML declaration, such as<?xml version="1.0"encoding="UTF-8"?>.

Parameters

NameTypeDescription
omitDeclarationBooleantrue to omit the XML declaration;false to include it

Return

Format — the formatter, for chaining


setOmitEncoding(omitEncoding)

Sets whether the formatter should omit the encoding in the XML declaration, such as theencoding field in<?xml version="1.0" encoding="UTF-8"?>.

Parameters

NameTypeDescription
omitEncodingBooleantrue to omit the encoding in the XML declaration;false to include it

Return

Format — the formatter, for chaining

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.