XML Service Stay organized with collections Save and categorize content based on your preferences.
Page Summary
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 createXML documents.
// Log the title and labels for the first page of blog posts on// Google's The Keyword blog.functionparseXml(){leturl='https://blog.google/rss/';letxml=UrlFetchApp.fetch(url).getContentText();letdocument=XmlService.parse(xml);letroot=document.getRootElement();letchannel=root.getChild('channel');letitems=channel.getChildren('item');items.forEach(item=>{lettitle=item.getChild('title').getText();letcategories=item.getChildren('category');letlabels=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.functioncreateXml(){letroot=XmlService.createElement('threads');letthreads=GmailApp.getInboxThreads()threads=threads.slice(0,10);// Just the first 10threads.forEach(thread=>{letchild=XmlService.createElement('thread').setAttribute('messageCount',thread.getMessageCount()).setAttribute('isUnread',thread.isUnread()).setText(thread.getFirstMessageSubject());root.addContent(child);});letdocument=XmlService.createDocument(root);letxml=XmlService.getPrettyFormat().format(document);console.log(xml);}
Classes
| Name | Brief description |
|---|---|
Attribute | A representation of an XML attribute. |
Cdata | A representation of an XMLCDATASection node. |
Comment | A representation of an XMLComment node. |
Content | A representation of a generic XML node. |
Content | An enumeration representing the types of XML content nodes. |
Doc | A representation of an XMLDocument node. |
Document | A representation of an XML document. |
Element | A representation of an XMLElement node. |
Entity | A representation of an XMLEntity node. |
Format | A formatter for outputting an XML document, with three pre-defined formats that can be furthercustomized. |
Namespace | A representation of an XML namespace. |
Processing | A representation of an XMLProcessing node. |
Text | A representation of an XMLText node. |
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 parentElement node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the text value of theText node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document. |
set | Text | Sets the text value of theText node. |
Comment
Methods
| Method | Return type | Brief description |
|---|---|---|
detach() | Content | Detaches the node from its parentElement node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the text value of theComment node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document. |
set | Comment | Sets the text value of theComment node. |
Content
Methods
| Method | Return type | Brief description |
|---|---|---|
as | Cdata | Casts the node as aCDATASection node for the purposes of autocomplete. |
as | Comment | Casts the node as aComment node for the purposes of autocomplete. |
as | Doc | Casts the node as aDocument node for the purposes of autocomplete. |
as | Element | Casts the node as anElement node for the purposes of autocomplete. |
as | Entity | Casts the node as aEntity node for the purposes of autocomplete. |
as | Processing | Casts the node as aProcessing node for the purposes of autocomplete. |
as | Text | Casts the node as aText node for the purposes of autocomplete. |
detach() | Content | Detaches the node from its parentElement node. |
get | Element | Gets the node's parentElement node. |
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 orderthey appear in the document. |
ContentType
Properties
| Property | Type | Description |
|---|---|---|
CDATA | Enum | An XMLCDATASection node. |
COMMENT | Enum | An XMLComment node. |
DOCTYPE | Enum | An XMLDocument node. |
ELEMENT | Enum | An XMLElement node. |
ENTITYREF | Enum | An XMLEntity node. |
PROCESSINGINSTRUCTION | Enum | An XMLProcessing node. |
TEXT | Enum | An XMLText node. |
DocType
Methods
| Method | Return type | Brief description |
|---|---|---|
detach() | Content | Detaches the node from its parentElement node. |
get | String | Gets the name of the rootElement node specified in theDoc declaration. |
get | String | Gets the internal subset data for theDocument node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the public ID of the external subset data for theDocument node. |
get | String | Gets the system ID of the external subset data for theDocument node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document. |
set | Doc | Sets the name of the rootElement node to specify in theDocdeclaration. |
set | Doc | Sets the internal subset data for theDocument node. |
set | Doc | Sets the public ID of the external subset data for theDocument node. |
set | Doc | Sets the system ID of the external subset data for theDocument node. |
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 thedocument. |
clone | Content[] | Creates unattached copies of all nodes that are immediate children of the document. |
detach | Element | Detaches and returns the document's rootElement node. |
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 thedocument. |
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 theyappear in the document. |
get | Doc | Gets the document'sDoc declaration. |
get | Element | Gets the document's rootElement node. |
has | Boolean | Determines whether the document has a rootElement node. |
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 thedocument. |
set | Document | Sets the document'sDoc declaration. |
set | Document | Sets the document's rootElement node. |
Element
Methods
| Method | Return type | Brief description |
|---|---|---|
add | Element | Appends the given node as the last child of theElement node. |
add | Element | Inserts the given node at the given index among all nodes that are immediate children of theElement node. |
clone | Content[] | Creates unattached copies of all nodes that are immediate children of the {@code Element} node. |
detach() | Content | Detaches the node from its parentElement node. |
get | Content[] | Gets all nodes that are immediate children of the {@code Element} node. |
get | Attribute | Gets the attribute for thisElement node with the given name and no namespace. |
get | Attribute | Gets the attribute for thisElement node with the given name and namespace. |
get | Attribute[] | Gets all attributes for thisElement node, in the order they appear in the document. |
get | Element | Gets the firstElement node with the given name and no namespace that is an immediatechild of thisElement node. |
get | Element | Gets the firstElement node with the given name and namespace that is an immediatechild of thisElement node. |
get | String | Gets the text value of the node with the given name and no namespace, if the node is animmediate child of theElement node. |
get | String | Gets the text value of the node with the given name and namespace, if the node is an immediatechild of theElement node. |
get | Element[] | Gets allElement nodes that are immediate children of thisElement node, in theorder they appear in the document. |
get | Element[] | Gets allElement nodes with the given name and no namespace that are immediate childrenof thisElement node, in the order they appear in the document. |
get | Element[] | Gets allElement nodes with the given name and namespace that are immediate children ofthisElement node, 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 theyappear in the document. |
get | Document | Gets the XML document that contains the {@code Element} node. |
get | String | Gets the local name of theElement node. |
get | Namespace | Gets the namespace for theElement node. |
get | Namespace | Gets the namespace with the given prefix for theElement node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the local name and namespace prefix of theElement node, in the form[namespacePrefix]:[localName]. |
get | String | Gets the text value of theElement node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document. |
is | Boolean | Determines whether thisElement node is a direct or indirect parent of a givenElement node. |
is | Boolean | Determines whether theElement node is the document's root node. |
remove | Boolean | Removes the given attribute for thisElement node, if such an attribute exists. |
remove | Boolean | Removes the attribute for thisElement node with the given name and no namespace, ifsuch an attribute exists. |
remove | Boolean | Removes the attribute for thisElement node with the given name and namespace, if suchan 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 thisElement node. |
set | Element | Sets the attribute for thisElement node with the given name, value, and no namespace. |
set | Element | Sets the attribute for thisElement node with the given name, value, and namespace. |
set | Element | Sets the local name of theElement node. |
set | Element | Sets the namespace for theElement node. |
set | Element | Sets the text value of theElement node. |
EntityRef
Methods
| Method | Return type | Brief description |
|---|---|---|
detach() | Content | Detaches the node from its parentElement node. |
get | String | Gets the name of theEntity node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the public ID of theEntity node. |
get | String | Gets the system ID of theEntity node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document. |
set | Entity | Sets the name of theEntity node. |
set | Entity | Sets the public ID of theEntity node. |
set | Entity | Sets the system ID of theEntity node. |
Format
Methods
| Method | Return type | Brief description |
|---|---|---|
format(document) | String | Outputs the givenDocument as a formatted string. |
format(element) | String | Outputs the givenElement node 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 theencoding 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 parentElement node. |
get | String | Gets the raw data for every instruction in theProcessing node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the target for theProcessing node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey 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 parentElement node. |
get | Element | Gets the node's parentElement node. |
get | String | Gets the text value of theText node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document. |
set | Text | Sets the text value of theText node. |
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 unattachedCDATASection node with the given value. |
create | Comment | Creates an unattachedComment node with the given value. |
create | Doc | Creates an unattachedDocument node for the rootElement nodewith the given name. |
create | Doc | Creates an unattachedDocument node for the rootElement nodewith the given name, and the given system ID for the external subset data. |
create | Doc | Creates an unattachedDocument node for the rootElement nodewith 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 rootElement node. |
create | Element | Creates an unattachedElement node with the given local name and no namespace. |
create | Element | Creates an unattachedElement node with the given local name and namespace. |
create | Text | Creates an unattachedText node with the given value. |
get | Format | Creates aFormat object for outputting a compact XML document. |
get | Namespace | Creates aNamespace with the given URI. |
get | Namespace | Creates aNamespace with the given prefix and URI. |
get | Namespace | Creates aNamespace that represents the absence of a real namespace. |
get | Format | Creates aFormat object for outputting a human-readable XML document. |
get | Format | Creates aFormat object for outputting a raw XML document. |
get | Namespace | Creates aNamespace with the standardxml prefix. |
parse(xml) | Document | Creates anDocument from the given XML, without validating the XML. |
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.