AI-generated Key Takeaways
- 
          The XML Service allows scripts to parse, navigate, and programmatically create XML documents. 
- 
          It provides classes like Document, Element, and Attribute for working with XML structure. 
- 
          The service includes methods for parsing XML strings, creating new XML elements and documents, and formatting XML output. 
- 
          You can navigate through XML elements using methods like getRootElement(), getChild(), and getChildren(). 
- 
          The service supports retrieving and setting text content and attributes of XML nodes. 
This service allows scripts to parse, navigate, and programmatically create XML documents.
// Log the title and labels for the first page of blog posts on // Google's The Keyword blog. function parseXml() { let url = 'https://blog.google/rss/'; let xml = UrlFetchApp.fetch(url).getContentText(); let document = XmlService.parse(xml); let root = document.getRootElement(); let channel = root.getChild('channel'); let items = channel.getChildren('item'); items.forEach(item => { let title = item.getChild('title').getText(); let categories = item.getChildren('category'); let labels = categories.map(category => category.getText()); console.log('%s (%s)', title, labels.join(', ')); }); } // Create and log an XML representation of first 10 threads in your Gmail inbox. function createXml() { let root = XmlService.createElement('threads'); let threads = GmailApp.getInboxThreads() threads = threads.slice(0,10); // Just the first 10 threads.forEach(thread => { let child = XmlService.createElement('thread') .setAttribute('messageCount', thread.getMessageCount()) .setAttribute('isUnread', thread.isUnread()) .setText(thread.getFirstMessageSubject()); root.addContent(child); }); let document = XmlService.createDocument(root); let xml = XmlService.getPrettyFormat().format(document); console.log(xml); }
Classes
| Name | Brief description | 
|---|---|
| Attribute | A representation of an XML attribute. | 
| Cdata | A representation of an XML CDATASectionnode. | 
| Comment | A representation of an XML Commentnode. | 
| Content | A representation of a generic XML node. | 
| Content | An enumeration representing the types of XML content nodes. | 
| Doc | A representation of an XML Documentnode. | 
| Document | A representation of an XML document. | 
| Element | A representation of an XML Elementnode. | 
| Entity | A representation of an XML Entitynode. | 
| Format | A formatter for outputting an XML document, with three pre-defined formats that can be further customized. | 
| Namespace | A representation of an XML namespace. | 
| Processing | A representation of an XML Processingnode. | 
| Text | A representation of an XML Textnode. | 
| Xml | This service allows scripts to parse, navigate, and programmatically create XML documents. | 
Attribute
Methods
| Method | Return type | Brief description | 
|---|---|---|
| get | String | Gets the local name of the attribute. | 
| get | Namespace | Gets the namespace for the attribute. | 
| get | String | Gets the value of the attribute. | 
| set | Attribute | Sets the local name of the attribute. | 
| set | Attribute | Sets the namespace for the attribute. | 
| set | Attribute | Sets the value of the attribute. | 
Cdata
Methods
| Method | Return type | Brief description | 
|---|---|---|
| append(text) | Text | Appends the given text to any content that already exists in the node. | 
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the text value of the Textnode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
| set | Text | Sets the text value of the Textnode. | 
Comment
Methods
| Method | Return type | Brief description | 
|---|---|---|
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the text value of the Commentnode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
| set | Comment | Sets the text value of the Commentnode. | 
Content
Methods
| Method | Return type | Brief description | 
|---|---|---|
| as | Cdata | Casts the node as a CDATASectionnode for the purposes of autocomplete. | 
| as | Comment | Casts the node as a Commentnode for the purposes of autocomplete. | 
| as | Doc | Casts the node as a Documentnode for the purposes of autocomplete. | 
| as | Element | Casts the node as an Elementnode for the purposes of autocomplete. | 
| as | Entity | Casts the node as a Entitynode for the purposes of autocomplete. | 
| as | Processing | Casts the node as a Processingnode for the purposes of autocomplete. | 
| as | Text | Casts the node as a Textnode for the purposes of autocomplete. | 
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | Content | Gets the node's content type. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
ContentType 
Properties
| Property | Type | Description | 
|---|---|---|
| CDATA | Enum | An XML CDATASectionnode. | 
| COMMENT | Enum | An XML Commentnode. | 
| DOCTYPE | Enum | An XML Documentnode. | 
| ELEMENT | Enum | An XML Elementnode. | 
| ENTITYREF | Enum | An XML Entitynode. | 
| PROCESSINGINSTRUCTION | Enum | An XML Processingnode. | 
| TEXT | Enum | An XML Textnode. | 
DocType 
Methods
| Method | Return type | Brief description | 
|---|---|---|
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | String | Gets the name of the root Elementnode specified in theDocdeclaration. | 
| get | String | Gets the internal subset data for the Documentnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the public ID of the external subset data for the Documentnode. | 
| get | String | Gets the system ID of the external subset data for the Documentnode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
| set | Doc | Sets the name of the root Elementnode to specify in theDocdeclaration. | 
| set | Doc | Sets the internal subset data for the Documentnode. | 
| set | Doc | Sets the public ID of the external subset data for the Documentnode. | 
| set | Doc | Sets the system ID of the external subset data for the Documentnode. | 
Document
Methods
| Method | Return type | Brief description | 
|---|---|---|
| add | Document | Appends the given node to the end of the document. | 
| add | Document | Inserts the given node at the given index among all nodes that are immediate children of the document. | 
| clone | Content[] | Creates unattached copies of all nodes that are immediate children of the document. | 
| detach | Element | Detaches and returns the document's root Elementnode. | 
| get | Content[] | Gets all nodes that are immediate children of the document. | 
| get | Content | Gets the node at the given index among all nodes that are immediate children of the document. | 
| get | Integer | Gets the number of nodes that are immediate children of the document. | 
| get | Content[] | Gets all nodes that are direct or indirect children of the document, in the order they appear in the document. | 
| get | Doc | Gets the document's Docdeclaration. | 
| get | Element | Gets the document's root Elementnode. | 
| has | Boolean | Determines whether the document has a root Elementnode. | 
| remove | Content[] | Removes all nodes that are immediate children of the document. | 
| remove | Boolean | Removes the given node, if the node is an immediate child of the document. | 
| remove | Content | Removes the node at the given index among all nodes that are immediate children of the document. | 
| set | Document | Sets the document's Docdeclaration. | 
| set | Document | Sets the document's root Elementnode. | 
Element
Methods
| Method | Return type | Brief description | 
|---|---|---|
| add | Element | Appends the given node as the last child of the Elementnode. | 
| add | Element | Inserts the given node at the given index among all nodes that are immediate children of the Elementnode. | 
| clone | Content[] | Creates unattached copies of all nodes that are immediate children of the {@code Element} node. | 
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | Content[] | Gets all nodes that are immediate children of the {@code Element} node. | 
| get | Attribute | Gets the attribute for this Elementnode with the given name and no namespace. | 
| get | Attribute | Gets the attribute for this Elementnode with the given name and namespace. | 
| get | Attribute[] | Gets all attributes for this Elementnode, in the order they appear in the document. | 
| get | Element | Gets the first Elementnode with the given name and no namespace that is an immediate
child of thisElementnode. | 
| get | Element | Gets the first Elementnode with the given name and namespace that is an immediate
child of thisElementnode. | 
| get | String | Gets the text value of the node with the given name and no namespace, if the node is an
immediate child of the Elementnode. | 
| get | String | Gets the text value of the node with the given name and namespace, if the node is an immediate
child of the Elementnode. | 
| get | Element[] | Gets all Elementnodes that are immediate children of thisElementnode, in the
order they appear in the document. | 
| get | Element[] | Gets all Elementnodes with the given name and no namespace that are immediate children
of thisElementnode, in the order they appear in the document. | 
| get | Element[] | Gets all Elementnodes with the given name and namespace that are immediate children of
thisElementnode, in the order they appear in the document. | 
| get | Content | Gets the node at the given index among all nodes that are immediate children of the {@code Element} node. | 
| get | Integer | Gets the number of nodes that are immediate children of the {@code Element} node. | 
| get | Content[] | Gets all nodes that are direct or indirect children of the {@code Element} node, in the order they appear in the document. | 
| get | Document | Gets the XML document that contains the {@code Element} node. | 
| get | String | Gets the local name of the Elementnode. | 
| get | Namespace | Gets the namespace for the Elementnode. | 
| get | Namespace | Gets the namespace with the given prefix for the Elementnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the local name and namespace prefix of the Elementnode, in the form[namespacePrefix]:[localName]. | 
| get | String | Gets the text value of the Elementnode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
| is | Boolean | Determines whether this Elementnode is a direct or indirect parent of a givenElementnode. | 
| is | Boolean | Determines whether the Elementnode is the document's root node. | 
| remove | Boolean | Removes the given attribute for this Elementnode, if such an attribute exists. | 
| remove | Boolean | Removes the attribute for this Elementnode with the given name and no namespace, if
such an attribute exists. | 
| remove | Boolean | Removes the attribute for this Elementnode with the given name and namespace, if such
an attribute exists. | 
| remove | Content[] | Removes all nodes that are immediate children of the {@code Element} node. | 
| remove | Boolean | Removes the given node, if the node is an immediate child of the {@code Element} node. | 
| remove | Content | Removes the node at the given index among all nodes that are immediate children of the {@code Element} node. | 
| set | Element | Sets the given attribute for this Elementnode. | 
| set | Element | Sets the attribute for this Elementnode with the given name, value, and no namespace. | 
| set | Element | Sets the attribute for this Elementnode with the given name, value, and namespace. | 
| set | Element | Sets the local name of the Elementnode. | 
| set | Element | Sets the namespace for the Elementnode. | 
| set | Element | Sets the text value of the Elementnode. | 
EntityRef 
Methods
| Method | Return type | Brief description | 
|---|---|---|
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | String | Gets the name of the Entitynode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the public ID of the Entitynode. | 
| get | String | Gets the system ID of the Entitynode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
| set | Entity | Sets the name of the Entitynode. | 
| set | Entity | Sets the public ID of the Entitynode. | 
| set | Entity | Sets the system ID of the Entitynode. | 
Format
Methods
| Method | Return type | Brief description | 
|---|---|---|
| format(document) | String | Outputs the given Documentas a formatted string. | 
| format(element) | String | Outputs the given Elementnode as a formatted string. | 
| set | Format | Sets the character encoding that the formatter should use. | 
| set | Format | Sets the string used to indent child nodes relative to their parents. | 
| set | Format | Sets the string to insert whenever the formatter would normally insert a line break. | 
| set | Format | Sets whether the formatter should omit the XML declaration, such as <?xml version="1.0"
encoding="UTF-8"?>. | 
| set | Format | Sets whether the formatter should omit the encoding in the XML declaration, such as the
encoding field in <?xml version="1.0" encoding="UTF-8"?>. | 
Namespace
Methods
| Method | Return type | Brief description | 
|---|---|---|
| get | String | Gets the prefix for the namespace. | 
| get | String | Gets the URI for the namespace. | 
ProcessingInstruction 
Methods
| Method | Return type | Brief description | 
|---|---|---|
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | String | Gets the raw data for every instruction in the Processingnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the target for the Processingnode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
Text
Methods
| Method | Return type | Brief description | 
|---|---|---|
| append(text) | Text | Appends the given text to any content that already exists in the node. | 
| detach() | Content | Detaches the node from its parent Elementnode. | 
| get | Element | Gets the node's parent Elementnode. | 
| get | String | Gets the text value of the Textnode. | 
| get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. | 
| set | Text | Sets the text value of the Textnode. | 
XmlService 
Properties
| Property | Type | Description | 
|---|---|---|
| Content | Content | An enumeration representing the types of XML content nodes. | 
Methods
| Method | Return type | Brief description | 
|---|---|---|
| create | Cdata | Creates an unattached CDATASectionnode with the given value. | 
| create | Comment | Creates an unattached Commentnode with the given value. | 
| create | Doc | Creates an unattached Documentnode for the rootElementnode
with the given name. | 
| create | Doc | Creates an unattached Documentnode for the rootElementnode
with the given name, and the given system ID for the external subset data. | 
| create | Doc | Creates an unattached Documentnode for the rootElementnode
with the given name, and the given public ID and system ID for the external subset data. | 
| create | Document | Creates an empty XML document. | 
| create | Document | Creates an XML document with the given root Elementnode. | 
| create | Element | Creates an unattached Elementnode with the given local name and no namespace. | 
| create | Element | Creates an unattached Elementnode with the given local name and namespace. | 
| create | Text | Creates an unattached Textnode with the given value. | 
| get | Format | Creates a Formatobject for outputting a compact XML document. | 
| get | Namespace | Creates a Namespacewith the given URI. | 
| get | Namespace | Creates a Namespacewith the given prefix and URI. | 
| get | Namespace | Creates a Namespacethat represents the absence of a real namespace. | 
| get | Format | Creates a Formatobject for outputting a human-readable XML document. | 
| get | Format | Creates a Formatobject for outputting a raw XML document. | 
| get | Namespace | Creates a Namespacewith the standardxmlprefix. | 
| parse(xml) | Document | Creates an Documentfrom the given XML, without validating the XML. |