{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":946,"path":"api-reference/server-api-reference/XMLStreamingAPIScopedAPI.md","title":"XMLStreamingAPI - Scoped","bundle":null,"doc_type":null,"release":"australia","body":"\n# XMLStreamingAPI- Scoped\n\nBuilds a large streaming XML payload for use in a REST or SOAP request to send bulk data to a third-party API. You can also create the payload as an XML string for a non-streaming option.\n\nUse these methods in a Workflow Studio Script step with the `sn_ih` namespace identifier. For example, you can use this API to create an XML payload in the Workflow Studio Script step and pass the returned value to the REST step to send the request to a third-party service. For more information, see the Workflow Studio [Script step](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/build-workflows/javascript-step-action-designer.md).\n\n**Note:** You can only use this API within the Workflow Studio environment.\n\nThere is no constructor for this class. Instead, you must call the build\\(\\) method in the [XMLStreamingBuilder](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/XMLStreamingBuilderScopedAPI.md) class to return an XMLStreamingAPI object.\n\n## API call order\n\nGenerate XML payloads by first instantiating a builder object with XMLStreamingBuilder and then calling the methods in the XMLStreamingAPI class:\n\n-   **1. XMLStreamingBuilder: Creates a builder object**\n\n    Use these method in the following order to create a builder object:\n\n    1.  XMLStreamingBuilder\\(\\): Instantiates the XMLStreamingBuilder object.\n    2.  withAttachment\\(\\): Optional. Creates an XML document as an attachment and stores it in the Streaming Attachments \\[streaming\\_attachment\\] table. If you don't call this method, the API builds the payload as an XML string.\n    3.  expiresAt\\(\\): Optional. Sets a time when the attachment expires. False is the default. Must also call the withAttachment\\(\\) method.\n    4.  build\\(\\): Returns an XMLStreamingAPI object.\n-   **2. XMLStreamingAPI: Builds the XML payload**\n\n    Use these methods in the following order to create the XML payload:\n\n    1.  startDocument\\(\\): Creates the top-level parent element in the XML document.\n    2.  Methods to generate child elements in the XML document, such as writeTextElement\\(\\), startElement\\(\\), and writeArray\\(\\).\n    3.  Methods to generate attributes for an element, such as writeAttribute\\(\\), writeNamespace\\(\\), and writeDtd\\(\\).\n    4.  endElement\\(\\): Closes an XML element.\n    5.  endDocument: Closes the top-level parent element.\n    6.  getXMLString\\(\\) or getAttachmentId\\(\\): Returns the XML string or attachment ID that you created.\n    7.  close\\(\\): Closes the XMLStreamingAPI object.\n\n## Size limits\n\nPayloads generated through this API cannot exceed these size limits:\n\n-   Attachments: 200 MB\n-   Strings: 5 MB\n\nThe following example shows how to create an XML document and store it in the Streaming Attachments \\[streaming\\_attachment\\] table with a defined expiration date.\n\n```\n\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment() // Creates the XML document in streaming mode within an attachment.\n    .expiresAt(ttl) // Sets an expiration date for the attachment.\n    .build(); // Creates the XMLStreamingAPI object.\n\n  builder.startDocument(\"Employee\") // Begins generating the XML document.\n    .writeTextElement(\"firstName\",\"John\") // Writes a \"firstName\" element and value.\n    .writeTextElement(\"lastName\",\"Smith\")\n    .writeTextElement(\"age\",\"25\")\n    .startElement(\"address\") // Adds an \"address\" parent element.\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\") // Writes a child element and value.\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement() // Adds a closing tag for the \"address\" element.\n    .startElement(\"phoneNumber\")\n      .writeTextElement(\"type\",\"home\")\n      .writeTextElement(\"number\",\"212 555-1234\")\n      .writeTextElement(\"type\",\"fax\")\n      .writeTextElement(\"number\",\"646 555-4567\")\n    .endElement()\n  .endDocument() // Stops generating the XML document.\n      \n  gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\nAlternatively, this example shows how to use the API in the Script step and create the payload as an XML string. You can use this option to create payloads under 5 MB.\n\n```\n(function execute(inputs, outputs) {\n\n  var builder = new sn_ih.XMLStreamingBuilder().build();\n  \n  builder.startDocument(\"Employee\")\n    .enablePrettyPrint()\n    .writeTextElement(\"firstName\",\"John\")\n    .writeTextElement(\"lastName\",\"Smith\")\n    .writeTextElement(\"age\",\"25\")\n    .startElement(\"address\")\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement()\n    .startElement(\"phoneNumber\")\n      .writeTextElement(\"type\",\"home\")\n      .writeTextElement(\"number\",\"212 555-1234\")\n      .writeTextElement(\"type\",\"fax\")\n      .writeTextElement(\"number\",\"646 555-4567\")\n    .endElement()\n  .endDocument()\n\n  outputs.payload = builder.getXMLString();\n  \n})(inputs, outputs);\n```\n\nOutput:\n\n```\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<firstName>John</firstName>\n<lastName>Smith</lastName>\n<age>25</age>\n<address>\n  <streetAddress>21 2nd Street</streetAddress>\n  <city>Santa Clara</city>\n  <state>CA</state>\n  <postalCode>11111</postalCode>\n</address>\n<phoneNumber>\n  <type>home</type>\n  <number>212 555-1234</number>\n  <type>fax</type>\n  <number>646 555-4567</number>\n</phoneNumber>\n```\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## XMLStreamingAPI - close\\(\\)\n\nCloses the XMLStreamingAPI object. You must call this method to close the stream after building your XML document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n### Example\n\nThe following example shows how to create an XML document and store it in the Streaming Attachments \\[streaming\\_attachment\\] table with a defined expiration date.\n\n```\n\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment() // Creates the XML document in streaming mode within an attachment.\n    .expiresAt(ttl) // Sets an expiration date for the attachment.\n    .build(); // Creates the XMLStreamingAPI object.\n\n  builder.startDocument(\"Employee\") // Begins generating the XML document.\n    .writeTextElement(\"firstName\",\"John\") // Writes a \"firstName\" element and value.\n    .writeTextElement(\"lastName\",\"Smith\")\n    .writeTextElement(\"age\",\"25\")\n    .startElement(\"address\") // Adds an \"address\" parent element.\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\") // Writes a child element and value.\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement() // Adds a closing tag for the \"address\" element.\n    .startElement(\"phoneNumber\")\n      .writeTextElement(\"type\",\"home\")\n      .writeTextElement(\"number\",\"212 555-1234\")\n      .writeTextElement(\"type\",\"fax\")\n      .writeTextElement(\"number\",\"646 555-4567\")\n    .endElement()\n  .endDocument() // Stops generating the XML document.\n      \n  gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - disablePrettyPrint\\(\\)\n\nEnds pretty print XML formatting.\n\nBefore calling this method, you must first call enablePrettyPrint\\(\\) to add XML formatting to a section.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to add pretty print formatting to the `address` element.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n    .writeTextElement(\"firstName\",\"John\")\n    .writeTextElement(\"lastName\",\"Smith\")\n    .writeTextElement(\"age\",\"25\")\n    .enablePrettyPrint()\n    .startElement(\"address\")\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement()\n    .disablePrettyPrint()\n    .startElement(\"phoneNumber\")\n      .writeTextElement(\"type\",\"home\")\n      .writeTextElement(\"number\",\"212 555-1234\")\n      .writeTextElement(\"type\",\"fax\")\n      .writeTextElement(\"number\",\"646 555-4567\")\n    .endElement()\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - enablePrettyPrint\\(\\)\n\nAdds pretty print formatting to an XML element or tree of elements.\n\nUse the disablePrettyPrint\\(\\) method to end the formatting.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to add pretty print formatting to the `address` element.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n    .writeTextElement(\"firstName\",\"John\")\n    .writeTextElement(\"lastName\",\"Smith\")\n    .writeTextElement(\"age\",\"25\")\n    .enablePrettyPrint()\n    .startElement(\"address\")\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement()\n    .disablePrettyPrint()\n    .startElement(\"phoneNumber\")\n      .writeTextElement(\"type\",\"home\")\n      .writeTextElement(\"number\",\"212 555-1234\")\n      .writeTextElement(\"type\",\"fax\")\n      .writeTextElement(\"number\",\"646 555-4567\")\n    .endElement()\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - endDocument\\(\\)\n\nEnds the structure of your XML document.\n\nAfter calling the startDocument\\(\\) method and organizing your streaming XML document, call the endDocument\\(\\) method at the end of your document's structure. You must use these two methods together to successfully build your streaming XML document's structure.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to create an XML document containing elements about a user.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n        .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - endElement\\(\\)\n\nAdds a closing tag to an XML element.\n\nUse the following methods in this sequence to create a valid XML element:\n\n1.  Use the startElement\\(\\) method to add a starting tag.\n2.  Use the endElement\\(\\) method to add the closing tag.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to build a parent element named `address` and then write four child elements.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n      .startElement(\"address\")\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement()\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - getXMLString\\(\\)\n\nReturns the XML document as a string.\n\nTo return the XML document as a string, don't call the getAttachementId\\(\\) method in the XMLStreamingBuilder class. For more information, see [XMLStreamingBuilder - Scoped](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/XMLStreamingBuilderScopedAPI.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|XML document created using the XMLStreamingAPI methods, as a string.|\n\nThe following example shows how to create an XML document and then return it as a string.\n\n```\n(function execute(inputs, outputs) {\n\n  var builder = new sn_ih.XMLStreamingBuilder().build();\n  \n  builder.startDocument(\"Employee\")\n    .enablePrettyPrint()\n    .writeTextElement(\"firstName\",\"John\")\n    .writeTextElement(\"lastName\",\"Smith\")\n    .writeTextElement(\"age\",\"25\")\n    .startElement(\"address\")\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement()\n    .startElement(\"phoneNumber\")\n      .writeTextElement(\"type\",\"home\")\n      .writeTextElement(\"number\",\"212 555-1234\")\n      .writeTextElement(\"type\",\"fax\")\n      .writeTextElement(\"number\",\"646 555-4567\")\n    .endElement()\n  .endDocument()\n\n  outputs.payload = builder.getXMLString();\n  \n})(inputs, outputs);\n```\n\nOutput:\n\n```\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<firstName>John</firstName>\n<lastName>Smith</lastName>\n<age>25</age>\n<address>\n  <streetAddress>21 2nd Street</streetAddress>\n  <city>Santa Clara</city>\n  <state>CA</state>\n  <postalCode>11111</postalCode>\n</address>\n<phoneNumber>\n  <type>home</type>\n  <number>212 555-1234</number>\n  <type>fax</type>\n  <number>646 555-4567</number>\n</phoneNumber>\n```\n\n## XMLStreamingAPI - startDocument\\(String rootElement, Object namespaceDefinitionMap\\)\n\nBegins building an XML document.\n\nAfter calling the build\\(\\) method, call the startDocument\\(\\) method to start organizing your XML document. You must also call the endDocument method at the end of your document's structure.\n\n<table id=\"table_yyj_tqg_dlb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nrootElement\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Root element, or top-level parent element, for your XML document.\n\n</td></tr><tr><td>\n\nnamespaceDefinitionMap\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. Map of keys and values for the namespaces and their associated values in a subsequent list of elements. For example: ```\n{\n'namespaceOne':'namespaceValue',\n'namespaceTwo':'namespaceValue'\n}\n```\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to create an XML document containing elements with information about an employee.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n        .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - startElement\\(String name, Object namespaceMap, Object attributeMap, String prefix\\)\n\nAdds a starting tag for an XML element.\n\nUse the following methods in this sequence to create a valid XML element:\n\n1.  Use the startElement\\(\\) method to add a starting tag.\n2.  Use the endElement\\(\\) method to add the closing tag.\n\n<table id=\"table_d1l_cjh_dlb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nName of the XML element.\n\n</td></tr><tr><td>\n\nnamespaceMap\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. Map of keys and values for the namespaces and their associated values in a subsequent list of elements. For example:```\n{\n'namespaceOne':'namespaceValue',\n'namespaceTwo':'namespaceValue'\n}\n```\n\n</td></tr><tr><td>\n\nattributeMap\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. Map of keys and values for the attributes and their associated values in a subsequent list of elements.\n\n</td></tr><tr><td>\n\nprefix\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Prefix for the XML element.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to build a parent element named address and then write four child elements.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n      .startElement(\"address\")\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n    .endElement()\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - writeArray\\(String elementName, Array data, String wrappingElement\\)\n\nAdds a list of nested elements with predefined text to your streaming XML document.\n\nAfter calling the startDocument\\(\\) method, you can call the writeArray\\(\\) method to add a block of nested elements to your streaming XML document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|elementName|String|Name of the XML element associated with each string listed in the **data** array.|\n|data|Array|List of values to assign to each element nested inside **wrappingElement**.|\n|wrappingElement|String|Parent element containing each **elementName**.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to build a parent element named `officeLocations`, and then nest an array of five `city` elements.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n      .writeArray('city', ['Santa Clara','San Diego','Chicago','Sydney','London'], 'officeLocations')\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - writeAttribute\\(String name, String value\\)\n\nAdds an attribute to an element in your XML document.\n\nAfter calling the startDocument\\(\\), startElement\\(\\), or writeTextElement\\(\\) method, you can call the writeAttribute\\(\\) method to add an attribute to the associated XML element.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Name of the XML element's attribute.|\n|value|String|Value for the XML element's attribute.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThis example adds an attribute named idNumber to the employee element.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n    .writeAttribute('idNumber','12345')\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeAttributes\\(Object attributeMap\\)\n\nAdds attributes to an element in your XML document.\n\nAfter calling the startDocument\\(\\), startElement\\(\\), or writeTextElement\\(\\) method, you can call the writeAttributes\\(\\) method to add attributes to the associated XML element.\n\n<table id=\"table_nzt_gbt_jlb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nattributeMap\n\n</td><td>\n\nObject\n\n</td><td>\n\nMap of keys and values containing attribute names and values to associate with the XML element. For example:```\n{\n'attributeOne':'attributeValue',\n'attributeTwo':'attributeValue'\n}\n```\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThis example adds attributes named idNumber, officeLocation, and department to the employee element.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n    .writeAttributes({'idNumber':'12345', 'officeLocation':'San Diego', 'department':'Sales'})\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeCData\\(String data\\)\n\nAdds CDATA to your XML document.\n\nAfter calling the writeCDataElement\\(\\) method, you can call the writeCData\\(\\) method to add CDATA within the element.\n\n|Name|Type|Description|\n|----|----|-----------|\n|data|String|Value to include after the CDATA keyword in your CDATA element.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThis example adds CDATA to the CDATA element `timeWorked`.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n        .writeCDataElement('timeWorked')\n        .writeCData('< 2 years')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeCDataElement\\(String name, String data, Object prefix\\)\n\nAdds a CDATA element to your XML document.\n\nAfter calling the startDocument\\(\\) method, you can call the writeCDataElement\\(\\) method to add a CDATA element to your XML document.\n\n<table id=\"table_z31_tbt_jlb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nName of the CDATA element.\n\n</td></tr><tr><td>\n\ndata\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Type of data to parse the CDATA element as.\n\n</td></tr><tr><td>\n\nprefix\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. Map of child elements and values that the CDATA element includes. For example: ```\n{\n'prefixOne':'prefixValue',\n'prefixTwo':'prefixValue'\n}\n```\n\nYou must associate an XML element's prefix with a namespace using [writeNamespace\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/XMLStreamingAPIScopedAPI.md).\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThis example uses a document type definition named `address` to define an internal document type definition for the XML document.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n  .writeDtd('address')\n  .writeCDataElement('home', '#PCDATA', {'streetAddress', 'city', 'state', 'postalCode'})\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - writeCharacters\\(String text\\)\n\nAdds text to your XML document.\n\nUse the writeCharacters\\(\\) method to insert string data to a section in your XML document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|Text to add to a section of your XML document.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to add text values to elements in your XML document.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n        .startElement('firstName')\n        .writeCharacters('John')\n        .EndElement()\n        .startElement('lastName')\n        .writeCharacters('Smith')\n        .endElement()\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeComment\\(String comment\\)\n\nAdds a comment to your XML document.\n\nAfter calling the startDocument\\(\\) method, you can call the writeComment\\(\\) method to add a comment to your XML document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|comment|String|Comment text to include.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to add a comment to an XML document.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n        .writeComment('Element for information related to active employees.')\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeDtd\\(String dtd\\)\n\nAdds a document type definition to your XML document.\n\nAfter calling the startDocument\\(\\) method, you can call the writeDtd\\(\\) method to add a valid XML document type definition to your XML document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|dtd|String|Name of a valid XML document type definition.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThis example uses a document type definition named address to define an internal document type definition for the XML document.\n\n```\ntry {\n  var ttl = new GlideDateTime(\"2011-01-01 12:00:00\");\n  var builder = new sn_ih.XMLStreamingBuilder()\n    .withAttachment()\n    .expiresAt(ttl)\n    .build();\n\n  builder.startDocument(\"Employee\")\n  .writeDtd('address')\n  .writeCDataElement('home', '#PCDATA', {'streetAddress', 'city', 'state', 'postalCode'})\n      .writeTextElement(\"streetAddress\", \"21 2nd Street\")\n      .writeTextElement(\"city\", \"Santa Clara\")\n      .writeTextElement(\"state\", \"CA\")\n      .writeTextElement(\"postalCode\", \"11111\")\n  .endDocument()\n      \n  gs.log(builder.getAttachmentId());\n} catch (err) {\n  gs.log(err);\n} finally {\n  builder.close();\n}\n```\n\n## XMLStreamingAPI - writeNamespace\\(String prefix, String namespaceURI\\)\n\nAdds a namespace to an element in your XML document.\n\nAfter calling the startDocument\\(\\), startElement\\(\\), or writeTextElement\\(\\) method, you can call the writeNamespace\\(\\) method to add a namespace to the associated XML element.\n\n|Name|Type|Description|\n|----|----|-----------|\n|prefix|String|Prefix for the XML namespace.|\n|namespaceURI|String|Optional. URI for the namespace.|\n\n|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to add a namespace and URI to the `company` root element, and then assign the prefix to the nested `companyName` element.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('company')\n    .writeNamespace('x','https://www.servicenow.com')\n        .writeTextElement('companyName', 'ServiceNow')\n        .writeNamespace('x')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeNamespaces\\(Object namespaceMap\\)\n\nAdds namespaces to the root element in your XML document.\n\nAfter calling the startDocument\\(\\) or startElement\\(\\) method, you can call the writeNamespaces\\(\\) method to declare namespaces for the associated XML element.\n\n<table id=\"table_z4l_21t_jlb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nnamespaceMap\n\n</td><td>\n\nObject\n\n</td><td>\n\nMap of keys and values containing namespace prefixes and URIs to associate with the root element of the XML document. For example: ```\n{\n'namespaceOne':'namespaceValue',\n'namespaceTwo':'namespaceValue'\n}\n```\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThis example adds two namespaces and URIs to the company root element, and then assigns the appropriate prefixes to the nested elements.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('company')\n    .writeNamespaces({'x':'https://www.servicenow.com', 'y':'https://www.developer.servicenow.com'})\n        .writeTextElement('companyName', 'ServiceNow')\n        .writeNamespace('x')\n        .writeTextElement('devFramework', 'UI Framework')\n        .writeNamespace('y')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n## XMLStreamingAPI - writeTextElement\\(String name, String text, Object prefix\\)\n\nAdds a single XML element to your XML document.\n\nAfter calling the startDocument\\(\\) method, you can call the writeTextElement\\(\\) method to add a single XML element to your XML document's structure.\n\n<table id=\"table_ryd_zkh_dlb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nName of the XML element.\n\n</td></tr><tr><td>\n\ntext\n\n</td><td>\n\nString\n\n</td><td>\n\nValue for the XML element.\n\n</td></tr><tr><td>\n\nprefix\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. Map of prefixes and values associated with the XML element. For example: ```\n{\n'prefixOne':'prefixValue',\n'prefixTwo':'prefixValue'\n}\n```\n\nYou must associate an XML element's prefix with a namespace using [writeNamespace\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/XMLStreamingAPIScopedAPI.md).\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|XMLStreamingAPI|Streaming XML object for constructing the payload.|\n\nThe following example shows how to create an XML document containing three elements with information about an employee.\n\n```\ntry {\n    var ttl = new GlideDateTime('2011-01-01 12:00:00');\n    var builder = new sn_ih.XMLStreamingBuilder().withAttachment().expiresAt(ttl);\n    var streamingDocument = builder.build();\n\n    streamingDocument.startDocument('Employee')\n        .writeTextElement('firstName', 'John')\n        .writeTextElement('lastName', 'Smith')\n        .writeTextElement('age', '25')\n    .endDocument();\n    gs.log(streamingDocument.getAttachmentId());\n} catch (err) {\n    gs.log(err);\n} finally {\n    streamingDocument.close();\n}\n```\n\n"}}