XML Service
-
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 CDATASection node. |
Comment | A representation of an XML Comment node. |
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 Element node. |
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 Text node. |
Xml | This service allows scripts to parse, navigate, and programmatically create XML documents. |
Attribute
Cdata
Content
ContentType
DocType
Document
Element
Methods
| Method | Return type | Brief description |
|---|---|---|
add | Element | Appends the given node as the last child of the Element node. |
add | Element | Inserts the given node at the given index among all nodes that are immediate children of the
Element node. |
clone | Content[] | Creates unattached copies of all nodes that are immediate children of the {@code Element} node. |
detach() | Content|null | Detaches the node from its parent Element node. |
get | Content[] | Gets all nodes that are immediate children of the {@code Element} node. |
get | Attribute|null | Gets the attribute for this Element node with the given name and no namespace. |
get | Attribute|null | Gets the attribute for this Element node with the given name and namespace. |
get | Attribute[] | Gets all attributes for this Element node, in the order they appear in the document. |
get | Element|null | Gets the first Element node with the given name and no namespace that is an immediate
child of this Element node. |
get | Element|null | Gets the first Element node with the given name and namespace that is an immediate
child of this Element node. |
get | String|null | Gets the text value of the node with the given name and no namespace, if the node is an
immediate child of the Element node. |
get | String|null | Gets the text value of the node with the given name and namespace, if the node is an immediate
child of the Element node. |
get | Element[] | Gets all Element nodes that are immediate children of this Element node, in the
order they appear in the document. |
get | Element[] | Gets all Element nodes with the given name and no namespace that are immediate children
of this Element node, in the order they appear in the document. |
get | Element[] | Gets all Element nodes with the given name and namespace that are immediate children of
this Element node, in the order they appear in the document. |
get | Content|null | 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 Element node. |
get | Namespace | Gets the namespace for the Element node. |
get | Namespace | Gets the namespace with the given prefix for the Element node. |
get | Element|null | Gets the node's parent Element node. |
get | String | Gets the local name and namespace prefix of the Element node, in the form [namespacePrefix]:[localName]. |
get | String | Gets the text value of the Element node. |
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 Element node is a direct or indirect parent of a given Element node. |
is | Boolean | Determines whether the Element node is the document's root node. |
remove | Boolean | Removes the given attribute for this Element node, if such an attribute exists. |
remove | Boolean | Removes the attribute for this Element node with the given name and no namespace, if
such an attribute exists. |
remove | Boolean | Removes the attribute for this Element node 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|null | 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 Element node. |
set | Element | Sets the attribute for this Element node with the given name, value, and no namespace. |
set | Element | Sets the attribute for this Element node with the given name, value, and namespace. |
set | Element | Sets the local name of the Element node. |
set | Element | Sets the namespace for the Element node. |
set | Element | Sets the text value of the Element node. |
EntityRef
Format
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|null | Detaches the node from its parent Element node. |
get | String | Gets the raw data for every instruction in the Processing node. |
get | Element|null | Gets the node's parent Element node. |
get | String | Gets the target for the Processing node. |
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. |