XMLStreamingBuilder - スコープ対象

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

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

    API 呼び出しの順序

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

    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:XMLStreamingBuilder()

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

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

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

    var builder = new sn_ih.XMLStreamingBuilder()

    XMLStreamingBuilder - build()

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

    表 : 2. パラメーター
    名前 タイプ 説明
    なし
    表 : 3. 戻り値
    タイプ 説明
    XML ストリーミング API ペイロードをビルドするためのストリーミング 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() メソッドも呼び出す必要があります。

    表 : 4. パラメーター
    名前 タイプ 説明
    expiresAt GlideDateTime 添付ファイルの有効期限が切れたときに設定されるオブジェクト。
    • 最小値:添付ファイルが作成されてから 7200 秒、つまり 2 時間。これは、 expiresAt() メソッドを呼び出さない場合のデフォルト値です。
    • 最大値:添付ファイルが作成されてから 172800 秒、つまり 48 時間。
    表 : 5. 戻り値
    タイプ 説明
    XML ストリーミングビルダー 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 ドキュメントを文字列として作成します。

    表 : 6. パラメーター
    名前 タイプ 説明
    なし
    表 : 7. 戻り値
    タイプ 説明
    XML ストリーミングビルダー 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>