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

NameBrief description
AttributeA representation of an XML attribute.
CdataA representation of an XML CDATASection node.
CommentA representation of an XML Comment node.
ContentA representation of a generic XML node.
ContentTypeAn enumeration representing the types of XML content nodes.
DocTypeA representation of an XML DocumentType node.
DocumentA representation of an XML document.
ElementA representation of an XML Element node.
EntityRefA representation of an XML EntityReference node.
FormatA formatter for outputting an XML document, with three pre-defined formats that can be further customized.
NamespaceA representation of an XML namespace.
ProcessingInstructionA representation of an XML ProcessingInstruction node.
TextA representation of an XML Text node.
XmlServiceThis service allows scripts to parse, navigate, and programmatically create XML documents.

Attribute

Cdata

Content

ContentType

DocType

Document

Element

Methods

MethodReturn typeBrief description
addContent(content)ElementAppends the given node as the last child of the Element node.
addContent(index, content)ElementInserts the given node at the given index among all nodes that are immediate children of the Element node.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the {@code Element} node.
detach()Content|nullDetaches the node from its parent Element node.
getAllContent()Content[]Gets all nodes that are immediate children of the {@code Element} node.
getAttribute(name)Attribute|nullGets the attribute for this Element node with the given name and no namespace.
getAttribute(name, namespace)Attribute|nullGets the attribute for this Element node with the given name and namespace.
getAttributes()Attribute[]Gets all attributes for this Element node, in the order they appear in the document.
getChild(name)Element|nullGets the first Element node with the given name and no namespace that is an immediate child of this Element node.
getChild(name, namespace)Element|nullGets the first Element node with the given name and namespace that is an immediate child of this Element node.
getChildText(name)String|nullGets the text value of the node with the given name and no namespace, if the node is an immediate child of the Element node.
getChildText(name, namespace)String|nullGets the text value of the node with the given name and namespace, if the node is an immediate child of the Element node.
getChildren()Element[]Gets all Element nodes that are immediate children of this Element node, in the order they appear in the document.
getChildren(name)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.
getChildren(name, namespace)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.
getContent(index)Content|nullGets the node at the given index among all nodes that are immediate children of the {@code Element} node.
getContentSize()IntegerGets the number of nodes that are immediate children of the {@code Element} node.
getDescendants()Content[]Gets all nodes that are direct or indirect children of the {@code Element} node, in the order they appear in the document.
getDocument()DocumentGets the XML document that contains the {@code Element} node.
getName()StringGets the local name of the Element node.
getNamespace()NamespaceGets the namespace for the Element node.
getNamespace(prefix)NamespaceGets the namespace with the given prefix for the Element node.
getParentElement()Element|nullGets the node's parent Element node.
getQualifiedName()StringGets the local name and namespace prefix of the Element node, in the form [namespacePrefix]:[localName].
getText()StringGets the text value of the Element node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
isAncestorOf(other)BooleanDetermines whether this Element node is a direct or indirect parent of a given Element node.
isRootElement()BooleanDetermines whether the Element node is the document's root node.
removeAttribute(attribute)BooleanRemoves the given attribute for this Element node, if such an attribute exists.
removeAttribute(attributeName)BooleanRemoves the attribute for this Element node with the given name and no namespace, if such an attribute exists.
removeAttribute(attributeName, namespace)BooleanRemoves the attribute for this Element node with the given name and namespace, if such an attribute exists.
removeContent()Content[]Removes all nodes that are immediate children of the {@code Element} node.
removeContent(content)BooleanRemoves the given node, if the node is an immediate child of the {@code Element} node.
removeContent(index)Content|nullRemoves the node at the given index among all nodes that are immediate children of the {@code Element} node.
setAttribute(attribute)ElementSets the given attribute for this Element node.
setAttribute(name, value)ElementSets the attribute for this Element node with the given name, value, and no namespace.
setAttribute(name, value, namespace)ElementSets the attribute for this Element node with the given name, value, and namespace.
setName(name)ElementSets the local name of the Element node.
setNamespace(namespace)ElementSets the namespace for the Element node.
setText(text)ElementSets the text value of the Element node.

EntityRef

Format

Namespace

Methods

MethodReturn typeBrief description
getPrefix()StringGets the prefix for the namespace.
getURI()StringGets the URI for the namespace.

ProcessingInstruction

Methods

MethodReturn typeBrief description
detach()Content|nullDetaches the node from its parent Element node.
getData()StringGets the raw data for every instruction in the ProcessingInstruction node.
getParentElement()Element|nullGets the node's parent Element node.
getTarget()StringGets the target for the ProcessingInstruction node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.

Text

XmlService

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-11 UTC.