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 Document node. | 
|  Document 
 | A representation of an XML document. | 
|  Element 
 | A representation of an XML Elementnode. | 
|  Entity  | A representation of an XML Entity node. | 
|  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 Processing node. | 
|  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 | 
|---|---|---|
| String | Gets the local name of the attribute. | |
|  Namespace 
 | Gets the namespace for the attribute. | |
| String | Gets the value of the attribute. | |
|  Attribute 
 | Sets the local name of the attribute. | |
|  Attribute 
 | Sets the namespace for the attribute. | |
|  Attribute 
 | Sets the value of the attribute. | 
  Cdata 
 
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Text 
 | Appends the given text to any content that already exists in the node. | |
|  Content 
 | Detaches the node from its parent  Element 
node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the text value of the Textnode. | |
| 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 
 | Sets the text value of the Textnode. | 
  Comment 
 
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Content 
 | Detaches the node from its parent  Element 
node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the text value of the Commentnode. | |
| 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. | |
|  Comment 
 | Sets the text value of the Commentnode. | 
  Content 
 
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Cdata 
 | Casts the node as a  CDATASection 
node for the purposes of autocomplete. | |
|  Comment 
 | Casts the node as a  Comment 
node for the purposes of autocomplete. | |
|  Doc  | Casts the node as a  Document node for the purposes of autocomplete. | |
|  Element 
 | Casts the node as an  Element 
node for the purposes of autocomplete. | |
|  Entity  | Casts the node as a  Entity node for the purposes of autocomplete. | |
|  Processing  | Casts the node as a  Processing node for the purposes of autocomplete. | |
|  Text 
 | Casts the node as a  Text 
node for the purposes of autocomplete. | |
|  Content 
 | Detaches the node from its parent  Element 
node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
|  Content  | Gets the node's content type. | |
| 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. | 
  Content  
 
 Properties
| Property | Type | Description | 
|---|---|---|
| CDATA | Enum | An XML  CDATASection 
node. | 
| COMMENT | Enum | An XML  Comment 
node. | 
| DOCTYPE | Enum | An XML  Document node. | 
| ELEMENT | Enum | An XML  Element 
node. | 
| ENTITYREF | Enum | An XML  Entity node. | 
| PROCESSINGINSTRUCTION | Enum | An XML  Processing node. | 
| TEXT | Enum | An XML  Text 
node. | 
  Doc  
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Content 
 | Detaches the node from its parent  Element 
node. | |
| String | Gets the name of the root  Element 
node specified in theDoc declaration. | |
| String | Gets the internal subset data for the Document node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the public ID of the external subset data for the Document node. | |
| String | Gets the system ID of the external subset data for the Document node. | |
| 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. | |
|  Doc  | Sets the name of the root  Element 
node to specify in theDoc declaration. | |
|  Doc  | Sets the internal subset data for the Document node. | |
|  Doc  | Sets the public ID of the external subset data for the Document node. | |
|  Doc  | Sets the system ID of the external subset data for the Document node. | 
  Document 
 
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Document 
 | Appends the given node to the end of the document. | |
|  Document 
 | Inserts the given node at the given index among all nodes that are immediate children of the document. | |
|  Content[] 
 | Creates unattached copies of all nodes that are immediate children of the document. | |
|  Element 
 | Detaches and returns the document's root  Element 
node. | |
|  Content[] 
 | Gets all nodes that are immediate children of the document. | |
|  Content 
 | Gets the node at the given index among all nodes that are immediate children of the document. | |
| Integer | Gets the number of nodes that are immediate children of the document. | |
|  Content[] 
 | Gets all nodes that are direct or indirect children of the document, in the order they appear in the document. | |
|  Doc  | Gets the document's  Doc declaration. | |
|  Element 
 | Gets the document's root  Element 
node. | |
| Boolean | Determines whether the document has a root  Element 
node. | |
|  Content[] 
 | Removes all nodes that are immediate children of the document. | |
| Boolean | Removes the given node, if the node is an immediate child of the document. | |
|  Content 
 | Removes the node at the given index among all nodes that are immediate children of the document. | |
|  Document 
 | Sets the document's  Doc declaration. | |
|  Document 
 | Sets the document's root  Element 
node. | 
  Element 
 
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Element 
 | Appends the given node as the last child of the Elementnode. | |
|  Element 
 | Inserts the given node at the given index among all nodes that are immediate children of the Elementnode. | |
|  Content[] 
 | Creates unattached copies of all nodes that are immediate children of the {@code Element} node. | |
|  Content 
 | Detaches the node from its parent  Element 
node. | |
|  Content[] 
 | Gets all nodes that are immediate children of the {@code Element} node. | |
|  Attribute 
 | Gets the attribute for this Elementnode with the given name and no namespace. | |
|  Attribute 
 | Gets the attribute for this Elementnode with the given name and namespace. | |
|  Attribute[] 
 | Gets all attributes for this Elementnode, in the order they appear in the document. | |
|  Element 
 | Gets the first Elementnode with the given name and no namespace that is an immediate
child of thisElementnode. | |
|  Element 
 | Gets the first Elementnode with the given name and namespace that is an immediate
child of thisElementnode. | |
| 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. | |
| String | Gets the text value of the node with the given name and namespace, if the node is an immediate
child of the Elementnode. | |
|  Element[] 
 | Gets all Elementnodes that are immediate children of thisElementnode, in the
order they appear in the document. | |
|  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. | |
|  Element[] 
 | Gets all Elementnodes with the given name and namespace that are immediate children of
thisElementnode, in the order they appear in the document. | |
|  Content 
 | Gets the node at the given index among all nodes that are immediate children of the {@code Element} node. | |
| Integer | Gets the number of nodes that are immediate children of the {@code Element} node. | |
|  Content[] 
 | Gets all nodes that are direct or indirect children of the {@code Element} node, in the order they appear in the document. | |
|  Document 
 | Gets the XML document that contains the {@code Element} node. | |
| String | Gets the local name of the Elementnode. | |
|  Namespace 
 | Gets the namespace for the Elementnode. | |
|  Namespace 
 | Gets the namespace with the given prefix for the Elementnode. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the local name and namespace prefix of the Elementnode, in the form[namespacePrefix]:[localName]. | |
| String | Gets the text value of the Elementnode. | |
| 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. | |
| Boolean | Determines whether this Elementnode is a direct or indirect parent of a givenElementnode. | |
| Boolean | Determines whether the Elementnode is the document's root node. | |
| Boolean | Removes the given attribute for this Elementnode, if such an attribute exists. | |
| Boolean | Removes the attribute for this Elementnode with the given name and no namespace, if
such an attribute exists. | |
| Boolean | Removes the attribute for this Elementnode with the given name and namespace, if such
an attribute exists. | |
|  Content[] 
 | Removes all nodes that are immediate children of the {@code Element} node. | |
| Boolean | Removes the given node, if the node is an immediate child of the {@code Element} node. | |
|  Content 
 | Removes the node at the given index among all nodes that are immediate children of the {@code Element} node. | |
|  Element 
 | Sets the given attribute for this Elementnode. | |
|  Element 
 | Sets the attribute for this Elementnode with the given name, value, and no namespace. | |
|  Element 
 | Sets the attribute for this Elementnode with the given name, value, and namespace. | |
|  Element 
 | Sets the local name of the Elementnode. | |
|  Element 
 | Sets the namespace for the Elementnode. | |
|  Element 
 | Sets the text value of the Elementnode. | 
  Entity  
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Content 
 | Detaches the node from its parent  Element 
node. | |
| String | Gets the name of the Entity node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the public ID of the Entity node. | |
| String | Gets the system ID of the Entity node. | |
| 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. | |
|  Entity  | Sets the name of the Entity node. | |
|  Entity  | Sets the public ID of the Entity node. | |
|  Entity  | Sets the system ID of the Entity node. | 
  Format 
 
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
| String | Outputs the given  Document 
as a formatted string. | |
| String | Outputs the given  Element 
node as a formatted string. | |
|  Format 
 | Sets the character encoding that the formatter should use. | |
|  Format 
 | Sets the string used to indent child nodes relative to their parents. | |
|  Format 
 | Sets the string to insert whenever the formatter would normally insert a line break. | |
|  Format 
 | Sets whether the formatter should omit the XML declaration, such as <?xml version="1.0"
encoding="UTF-8"?>. | |
|  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 | 
|---|---|---|
| String | Gets the prefix for the namespace. | |
| String | Gets the URI for the namespace. | 
  Processing  
 
 Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Content 
 | Detaches the node from its parent  Element 
node. | |
| String | Gets the raw data for every instruction in the Processing node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the target for the Processing node. | |
| 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 | 
|---|---|---|
|  Text 
 | Appends the given text to any content that already exists in the node. | |
|  Content 
 | Detaches the node from its parent  Element 
node. | |
|  Element 
 | Gets the node's parent  Element 
node. | |
| String | Gets the text value of the Textnode. | |
| 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 
 | Sets the text value of the Textnode. | 
  Xml  
 
 Properties
| Property | Type | Description | 
|---|---|---|
| Content  |  Content  | An enumeration representing the types of XML content nodes. | 
Methods
| Method | Return type | Brief description | 
|---|---|---|
|  Cdata 
 | Creates an unattached  CDATASection 
node with the given value. | |
|  Comment 
 | Creates an unattached  Comment 
node with the given value. | |
|  Doc  | Creates an unattached  Document node for the root Element 
node
with the given name. | |
|  Doc  | Creates an unattached  Document node for the root Element 
node
with the given name, and the given system ID for the external subset data. | |
|  Doc  | Creates an unattached  Document node for the root Element 
node
with the given name, and the given public ID and system ID for the external subset data. | |
|  Document 
 | Creates an empty XML document. | |
|  Document 
 | Creates an XML document with the given root  Element 
node. | |
|  Element 
 | Creates an unattached  Element 
node with the given local name and no namespace. | |
|  Element 
 | Creates an unattached  Element 
node with the given local name and namespace. | |
|  Text 
 | Creates an unattached  Text 
node with the given value. | |
|  Format 
 | Creates a  Format 
object for outputting a compact XML document. | |
|  Namespace 
 | Creates a  Namespace 
with the given URI. | |
|  Namespace 
 | Creates a  Namespace 
with the given prefix and URI. | |
|  Namespace 
 | Creates a  Namespace 
that represents the absence of a real namespace. | |
|  Format 
 | Creates a  Format 
object for outputting a human-readable XML document. | |
|  Format 
 | Creates a  Format 
object for outputting a raw XML document. | |
|  Namespace 
 | Creates a  Namespace 
with the standardxmlprefix. | |
|  Document 
 | Creates an  Document 
from the given XML, without validating the XML. | 

