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 version="1.0"
encoding="UTF-8"?> などの XML 宣言を省略するかどうかを設定します。 |
set | Format | フォーマッタが XML 宣言のエンコード(<?xml version="1.0" encoding="UTF-8"?> のエンコード フィールドなど)を省略するかどうかを設定します。 |
詳細なドキュメント
format(document)
format(element)
set Encoding(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 - チェーン用のフォーマッタ。
set Indent(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 - チェーン用のフォーマッタ。
set Line Separator(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 - チェーン用のフォーマッタ。
set Omit Declaration(omitDeclaration)
フォーマッタが <?xml version="1.0"
encoding="UTF-8"?> などの XML 宣言を省略するかどうかを設定します。
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
omit | Boolean | true は XML 宣言を省略し、false は XML 宣言を含めます。 |
戻る
Format - チェーン用のフォーマッタ。
set Omit Encoding(omitEncoding)
フォーマッタが XML 宣言のエンコード(<?xml version="1.0" encoding="UTF-8"?> のエンコード フィールドなど)を省略するかどうかを設定します。
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
omit | Boolean | true: XML 宣言でエンコードを省略します。false: エンコードを含めます。 |
戻る
Format - チェーン用のフォーマッタ。