XMLStreamingBuilder - スコープ指定

  • リリースバージョン: Washingtondc
  • 更新日 2024年02月01日
  • 読む18読むのに数分
  • 大規模な XML ペイロードを構築するためのビルダーオブジェクトを作成し、それを REST または SOAP 要求で使用して大量のデータをサードパーティ API に送信します。非ストリーミングオプション用の XML 文字列としてペイロードを作成することもできます。

    これらのメソッドを フローデザイナー スクリプトステップで sn_ih 名前空間識別子とともに使用します。たとえば、この API を使用して フローデザイナー スクリプトステップで XML ペイロードを作成し、戻り値を REST ステップに渡して、要求をサードパーティサービスに送信できます。詳細については、「フローデザイナー スクリプトステップ」を参照してください。
    注:
    この API は フローデザイナー 環境内でのみ使用できます。

    API 呼び出し順

    まず XMLStreamingBuilder でビルダーオブジェクトをインスタンス化し、次に XMLStreamingAPI クラスで以下のメソッドを呼び出して XML ペイロードを生成します。

    1. XMLStreamingBuilder:ビルダーオブジェクトを作成する
    下記のメソッドを次の順番で使用して、ビルダーオブジェクトを作成します。
    1. XMLStreamingBuilder()XMLStreamingBuilder オブジェクトをインスタンス化します。
    2. withAttachment():オプション。添付ファイルとして XML ドキュメントを作成し、ストリーミング添付ファイル [streaming_attachment] テーブルに格納します。このメソッドを呼び出さない場合、API はペイロードを XML 文字列として構築します。
    3. expiresAt():オプション。添付ファイルの有効期限が切れる時刻を設定します。デフォルトは false です。withAttachment() メソッドも呼び出す必要があります。
    4. build()XMLStreamingAPI オブジェクトを返します。
    2. XMLStreamingAPI:XML ペイロードを構築する
    下記のメソッドを次の順番で使用して、XML ペイロードを作成します。
    1. startDocument()XML ドキュメントの最上位の親要素を作成します。
    2. XML ドキュメントに子要素を生成するメソッド:writeTextElement()startElement()writeArray() など。
    3. 要素の属性を生成するメソッド:writeAttribute()writeNamespace()writeDtd() など。
    4. endElement()XML 要素を閉じます。
    5. endDocument最上位の親要素を閉じます。
    6. getXMLString() または getAttachmentId():作成した XML 文字列または添付ファイル ID を返します。
    7. close()XMLStreamingAPI オブジェクトを閉じます。

    サイズ制限

    この API を介して生成されたペイロードは、次のサイズ制限を超えることはできません。

    • 添付ファイル:200 MB
    • 文字列:5 MB

    次の例では、XML ドキュメントを作成し、定義された有効期限と一緒にストリーミング添付ファイル [streaming_attachment] テーブルに格納する方法を示します。

    
    try {
      var ttl = new GlideDateTime("2011-01-01 12:00:00");
      var builder = new sn_ih.XMLStreamingBuilder()
        .withAttachment() // Creates the XML document in streaming mode within an attachment.
        .expiresAt(ttl) // Sets an expiration date for the attachment.
        .build(); // Creates the XMLStreamingAPI object.
    
      builder.startDocument("Employee") // Begins generating the XML document.
        .writeTextElement("firstName","John") // Writes a "firstName" element and value.
        .writeTextElement("lastName","Smith")
        .writeTextElement("age","25")
        .startElement("address") // Adds an "address" parent element.
          .writeTextElement("streetAddress", "21 2nd Street") // Writes a child element and value.
          .writeTextElement("city", "Santa Clara")
          .writeTextElement("state", "CA")
          .writeTextElement("postalCode", "11111")
        .endElement() // Adds a closing tag for the "address" element.
        .startElement("phoneNumber")
          .writeTextElement("type","home")
          .writeTextElement("number","212 555-1234")
          .writeTextElement("type","fax")
          .writeTextElement("number","646 555-4567")
        .endElement()
      .endDocument() // Stops generating the XML document.
          
      gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
    } catch (err) {
      gs.log(err);
    } finally {
      builder.close();
    }

    一方この例は、スクリプトステップで API を使用してペイロードを XML 文字列として作成する方法を示しています。このオプションを使用すると、5 MB 未満のペイロードを作成できます。

    (function execute(inputs, outputs) {
    
      var builder = new sn_ih.XMLStreamingBuilder().build();
      
      builder.startDocument("Employee")
        .enablePrettyPrint()
        .writeTextElement("firstName","John")
        .writeTextElement("lastName","Smith")
        .writeTextElement("age","25")
        .startElement("address")
          .writeTextElement("streetAddress", "21 2nd Street")
          .writeTextElement("city", "Santa Clara")
          .writeTextElement("state", "CA")
          .writeTextElement("postalCode", "11111")
        .endElement()
        .startElement("phoneNumber")
          .writeTextElement("type","home")
          .writeTextElement("number","212 555-1234")
          .writeTextElement("type","fax")
          .writeTextElement("number","646 555-4567")
        .endElement()
      .endDocument()
    
      outputs.payload = builder.getXMLString();
      
    })(inputs, outputs);

    出力:

    <?xml version="1.0" encoding="UTF-8"?>
    <firstName>John</firstName>
    <lastName>Smith</lastName>
    <age>25</age>
    <address>
      <streetAddress>21 2nd Street</streetAddress>
      <city>Santa Clara</city>
      <state>CA</state>
      <postalCode>11111</postalCode>
    </address>
    <phoneNumber>
      <type>home</type>
      <number>212 555-1234</number>
      <type>fax</type>
      <number>646 555-4567</number>
    </phoneNumber>

    XMLStreamingBuilder - build()

    XMLStreamingAPI オブジェクトを返します。

    表 : 1. パラメーター
    名前 タイプ 説明
    なし
    表 : 2. 返される内容
    タイプ 説明
    XMLStreamingAPI ペイロードを構築するためのストリーミング XML オブジェクト。

    次の例では、XML ドキュメントを作成し、定義された有効期限と一緒にストリーミング添付ファイル [streaming_attachment] テーブルに格納する方法を示します。

    
    try {
      var ttl = new GlideDateTime("2011-01-01 12:00:00");
      var builder = new sn_ih.XMLStreamingBuilder()
        .withAttachment() // Creates the XML document in streaming mode within an attachment.
        .expiresAt(ttl) // Sets an expiration date for the attachment.
        .build(); // Creates the XMLStreamingAPI object.
    
      builder.startDocument("Employee") // Begins generating the XML document.
        .writeTextElement("firstName","John") // Writes a "firstName" element and value.
        .writeTextElement("lastName","Smith")
        .writeTextElement("age","25")
        .startElement("address") // Adds an "address" parent element.
          .writeTextElement("streetAddress", "21 2nd Street") // Writes a child element and value.
          .writeTextElement("city", "Santa Clara")
          .writeTextElement("state", "CA")
          .writeTextElement("postalCode", "11111")
        .endElement() // Adds a closing tag for the "address" element.
        .startElement("phoneNumber")
          .writeTextElement("type","home")
          .writeTextElement("number","212 555-1234")
          .writeTextElement("type","fax")
          .writeTextElement("number","646 555-4567")
        .endElement()
      .endDocument() // Stops generating the XML document.
          
      gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
    } catch (err) {
      gs.log(err);
    } finally {
      builder.close();
    }

    XMLStreamingBuilder - expiresAt(オブジェクト expiresAt)

    添付ファイルの有効期限が切れる時刻を設定します。withAttachment() メソッドも呼び出す必要があります。

    表 : 3. パラメーター
    名前 タイプ 説明
    expiresAt GlideDateTime 添付ファイルの有効期限が切れるまでの時間を設定するオブジェクト。
    • 最小値:添付ファイルが作成されてから 7200 秒 (2 時間)。これは、expiresAt() メソッドを呼び出さない場合のデフォルト値です。
    • 最大値:添付ファイルが作成されてから 172800 秒 (48 時間)。
    表 : 4. 返される内容
    タイプ 説明
    XMLStreamingBuilder XML ペイロードを開始するために使用するビルダーオブジェクト。

    次の例では、XML ドキュメントを作成し、定義された有効期限と一緒にストリーミング添付ファイル [streaming_attachment] テーブルに格納する方法を示します。

    
    try {
      var ttl = new GlideDateTime("2011-01-01 12:00:00");
      var builder = new sn_ih.XMLStreamingBuilder()
        .withAttachment() // Creates the XML document in streaming mode within an attachment.
        .expiresAt(ttl) // Sets an expiration date for the attachment.
        .build(); // Creates the XMLStreamingAPI object.
    
      builder.startDocument("Employee") // Begins generating the XML document.
        .writeTextElement("firstName","John") // Writes a "firstName" element and value.
        .writeTextElement("lastName","Smith")
        .writeTextElement("age","25")
        .startElement("address") // Adds an "address" parent element.
          .writeTextElement("streetAddress", "21 2nd Street") // Writes a child element and value.
          .writeTextElement("city", "Santa Clara")
          .writeTextElement("state", "CA")
          .writeTextElement("postalCode", "11111")
        .endElement() // Adds a closing tag for the "address" element.
        .startElement("phoneNumber")
          .writeTextElement("type","home")
          .writeTextElement("number","212 555-1234")
          .writeTextElement("type","fax")
          .writeTextElement("number","646 555-4567")
        .endElement()
      .endDocument() // Stops generating the XML document.
          
      gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
    } catch (err) {
      gs.log(err);
    } finally {
      builder.close();
    }

    XMLStreamingBuilder - withAttachment()

    添付ファイルとして XML ドキュメントを作成し、ストリーミング添付ファイル [streaming_attachment] テーブルに格納します。このメソッドを呼び出さない場合、API は XML ドキュメントを文字列として作成します。

    表 : 5. パラメーター
    名前 タイプ 説明
    なし
    表 : 6. 返される内容
    タイプ 説明
    XMLStreamingBuilder XML ペイロードを開始するために使用するビルダーオブジェクト。

    次の例では、XML ドキュメントを作成し、定義された有効期限と一緒にストリーミング添付ファイル [streaming_attachment] テーブルに格納する方法を示します。

    
    try {
      var ttl = new GlideDateTime("2011-01-01 12:00:00");
      var builder = new sn_ih.XMLStreamingBuilder()
        .withAttachment() // Creates the XML document in streaming mode within an attachment.
        .expiresAt(ttl) // Sets an expiration date for the attachment.
        .build(); // Creates the XMLStreamingAPI object.
    
      builder.startDocument("Employee") // Begins generating the XML document.
        .writeTextElement("firstName","John") // Writes a "firstName" element and value.
        .writeTextElement("lastName","Smith")
        .writeTextElement("age","25")
        .startElement("address") // Adds an "address" parent element.
          .writeTextElement("streetAddress", "21 2nd Street") // Writes a child element and value.
          .writeTextElement("city", "Santa Clara")
          .writeTextElement("state", "CA")
          .writeTextElement("postalCode", "11111")
        .endElement() // Adds a closing tag for the "address" element.
        .startElement("phoneNumber")
          .writeTextElement("type","home")
          .writeTextElement("number","212 555-1234")
          .writeTextElement("type","fax")
          .writeTextElement("number","646 555-4567")
        .endElement()
      .endDocument() // Stops generating the XML document.
          
      gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
    } catch (err) {
      gs.log(err);
    } finally {
      builder.close();
    }

    この例は、XML ペイロードを構築して文字列として保存する方法を示しています。

    (function execute(inputs, outputs) {
    
      var builder = new sn_ih.XMLStreamingBuilder().build();
      
      builder.startDocument("Employee")
        .enablePrettyPrint()
        .writeTextElement("firstName","John")
        .writeTextElement("lastName","Smith")
        .writeTextElement("age","25")
        .startElement("address")
          .writeTextElement("streetAddress", "21 2nd Street")
          .writeTextElement("city", "Santa Clara")
          .writeTextElement("state", "CA")
          .writeTextElement("postalCode", "11111")
        .endElement()
        .startElement("phoneNumber")
          .writeTextElement("type","home")
          .writeTextElement("number","212 555-1234")
          .writeTextElement("type","fax")
          .writeTextElement("number","646 555-4567")
        .endElement()
      .endDocument()
    
      outputs.payload = builder.getXMLString();
      
    })(inputs, outputs);

    出力:

    <?xml version="1.0" encoding="UTF-8"?>
    <firstName>John</firstName>
    <lastName>Smith</lastName>
    <age>25</age>
    <address>
      <streetAddress>21 2nd Street</streetAddress>
      <city>Santa Clara</city>
      <state>CA</state>
      <postalCode>11111</postalCode>
    </address>
    <phoneNumber>
      <type>home</type>
      <number>212 555-1234</number>
      <type>fax</type>
      <number>646 555-4567</number>
    </phoneNumber>

    XMLStreamingBuilder - XMLStreamingBuilder()

    XMLStreamingBuilder オブジェクトをインスタンス化します。

    表 : 7. パラメーター
    名前 タイプ 説明
    なし

    この例では、builder オブジェクトをインスタンス化する方法を示します。

    var builder = new sn_ih.XMLStreamingBuilder()