XML ドキュメントを出力するためのフォーマッタ。3 つの事前定義された形式があり、さらにカスタマイズできます。
// 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);
メソッド
| メソッド | 戻り値の型 | 概要 |
|---|---|---|
format(document) | String | 指定された Document を書式設定された文字列として出力します。 |
format(element) | String | 指定された Element ノードを書式設定された文字列として出力します。 |
set | Format | フォーマッタが使用する文字エンコードを設定します。 |
set | Format | 親ノードに対する子ノードのインデントに使用する文字列を設定します。 |
set | Format | フォーマッタが通常改行を挿入するたびに挿入する文字列を設定します。 |
set | Format | フォーマッタが XML 宣言(<?xml version="1.0"
encoding="UTF-8"?> など)を省略するかどうかを設定します。 |
set | Format | フォーマッタが XML 宣言のエンコード(
<?xml version="1.0" encoding="UTF-8"?> のエンコード フィールドなど)を省略するかどうかを設定します。 |
詳細なドキュメント
format(document)
format(element)
setEncoding(encoding)
フォーマッタが使用する文字エンコードを設定します。encoding 引数は、ISO-8859-1、US-ASCII、UTF-8、UTF-16 などの許容される XML エンコードである必要があります。
// Log an XML document with encoding that does not support certain special // characters. const xml = '<root><a><b>ಠ‿ಠ</b><b>ಠ‿ಠ</b></a></root>'; const document = XmlService.parse(xml); const output = XmlService.getRawFormat().setEncoding('ISO-8859-1').format(document); Logger.log(output);
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
encoding | String | 使用するエンコード。 |
戻る
Format - チェーン用のフォーマッタ。
setIndent(indent)
親ノードに対する子ノードのインデントに使用する文字列を設定します。null 以外のインデントを設定すると、フォーマッタはすべてのノードの後に改行を挿入します。
// Log an XML document with each child node indented four spaces. const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const document = XmlService.parse(xml); const output = XmlService.getCompactFormat().setIndent(' ').format(document); Logger.log(output);
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
indent | String | 使用するインデント。 |
戻る
Format - チェーン用のフォーマッタ。
setLineSeparator(separator)
フォーマッタが通常改行を挿入するたびに挿入する文字列を設定します。事前定義された 3 つのフォーマッタでは、改行を挿入する条件が異なります。デフォルトの行区切り文字は \r\n です。
// Log an XML document with several spaces and a pipe character in place of line // breaks. const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const document = XmlService.parse(xml); const output = XmlService.getRawFormat().setLineSeparator(' | ').format(document); Logger.log(output);
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
separator | String | 使用する区切り文字。 |
戻る
Format - チェーン用のフォーマッタ。
setOmitDeclaration(omitDeclaration)
フォーマッタが XML 宣言(<?xml version="1.0"
encoding="UTF-8"?> など)を省略するかどうかを設定します。
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
omit | Boolean | XML 宣言を省略する場合は true、含める場合は false。 |
戻る
Format - チェーン用のフォーマッタ。
setOmitEncoding(omitEncoding)
フォーマッタが XML 宣言のエンコード(
<?xml version="1.0" encoding="UTF-8"?> のエンコード フィールドなど)を省略するかどうかを設定します。
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
omit | Boolean | XML 宣言のエンコードを省略する場合は true、含める場合は false。 |
戻る
Format - チェーン用のフォーマッタ。