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.

XML Service

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

NameBrief description
AttributeA representation of an XML attribute.
CdataA representation of an XMLCDATASection node.
CommentA representation of an XMLComment node.
ContentA representation of a generic XML node.
ContentTypeAn enumeration representing the types of XML content nodes.
DocTypeA representation of an XMLDocumentType node.
DocumentA representation of an XML document.
ElementA representation of an XMLElement node.
EntityRefA representation of an XMLEntityReference node.
FormatA formatter for outputting an XML document, with three pre-defined formats that can be furthercustomized.
NamespaceA representation of an XML namespace.
ProcessingInstructionA representation of an XMLProcessingInstruction node.
TextA representation of an XMLText node.
XmlServiceThis service allows scripts to parse, navigate, and programmatically create XML documents.

Attribute

Methods

MethodReturn typeBrief description
getName()StringGets the local name of the attribute.
getNamespace()NamespaceGets the namespace for the attribute.
getValue()StringGets the value of the attribute.
setName(name)AttributeSets the local name of the attribute.
setNamespace(namespace)AttributeSets the namespace for the attribute.
setValue(value)AttributeSets the value of the attribute.

Cdata

Methods

MethodReturn typeBrief description
append(text)TextAppends the given text to any content that already exists in the node.
detach()ContentDetaches the node from its parentElement node.
getParentElement()ElementGets the node's parentElement node.
getText()StringGets the text value of theText node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
setText(text)TextSets the text value of theText node.

Comment

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parentElement node.
getParentElement()ElementGets the node's parentElement node.
getText()StringGets the text value of theComment node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
setText(text)CommentSets the text value of theComment node.

Content

Methods

MethodReturn typeBrief description
asCdata()CdataCasts the node as aCDATASection node for the purposes of autocomplete.
asComment()CommentCasts the node as aComment node for the purposes of autocomplete.
asDocType()DocTypeCasts the node as aDocumentType node for the purposes of autocomplete.
asElement()ElementCasts the node as anElement node for the purposes of autocomplete.
asEntityRef()EntityRefCasts the node as aEntityReference node for the purposes of autocomplete.
asProcessingInstruction()ProcessingInstructionCasts the node as aProcessingInstruction node for the purposes of autocomplete.
asText()TextCasts the node as aText node for the purposes of autocomplete.
detach()ContentDetaches the node from its parentElement node.
getParentElement()ElementGets the node's parentElement node.
getType()ContentTypeGets the node's content type.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.

ContentType

Properties

PropertyTypeDescription
CDATAEnumAn XMLCDATASection node.
COMMENTEnumAn XMLComment node.
DOCTYPEEnumAn XMLDocumentType node.
ELEMENTEnumAn XMLElement node.
ENTITYREFEnumAn XMLEntityReference node.
PROCESSINGINSTRUCTIONEnumAn XMLProcessingInstruction node.
TEXTEnumAn XMLText node.

DocType

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parentElement node.
getElementName()StringGets the name of the rootElement node specified in theDocType declaration.
getInternalSubset()StringGets the internal subset data for theDocumentType node.
getParentElement()ElementGets the node's parentElement node.
getPublicId()StringGets the public ID of the external subset data for theDocumentType node.
getSystemId()StringGets the system ID of the external subset data for theDocumentType node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
setElementName(name)DocTypeSets the name of the rootElement node to specify in theDocTypedeclaration.
setInternalSubset(data)DocTypeSets the internal subset data for theDocumentType node.
setPublicId(id)DocTypeSets the public ID of the external subset data for theDocumentType node.
setSystemId(id)DocTypeSets the system ID of the external subset data for theDocumentType node.

Document

Methods

MethodReturn typeBrief description
addContent(content)DocumentAppends the given node to the end of the document.
addContent(index, content)DocumentInserts the given node at the given index among all nodes that are immediate children of thedocument.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the document.
detachRootElement()ElementDetaches and returns the document's rootElement node.
getAllContent()Content[]Gets all nodes that are immediate children of the document.
getContent(index)ContentGets the node at the given index among all nodes that are immediate children of thedocument.
getContentSize()IntegerGets the number of nodes that are immediate children of the document.
getDescendants()Content[]Gets all nodes that are direct or indirect children of the document, in the order theyappear in the document.
getDocType()DocTypeGets the document'sDocType declaration.
getRootElement()ElementGets the document's rootElement node.
hasRootElement()BooleanDetermines whether the document has a rootElement node.
removeContent()Content[]Removes all nodes that are immediate children of the document.
removeContent(content)BooleanRemoves the given node, if the node is an immediate child of the document.
removeContent(index)ContentRemoves the node at the given index among all nodes that are immediate children of thedocument.
setDocType(docType)DocumentSets the document'sDocType declaration.
setRootElement(element)DocumentSets the document's rootElement node.

Element

Methods

MethodReturn typeBrief description
addContent(content)ElementAppends the given node as the last child of theElement node.
addContent(index, content)ElementInserts the given node at the given index among all nodes that are immediate children of theElement node.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the {@code Element} node.
detach()ContentDetaches the node from its parentElement node.
getAllContent()Content[]Gets all nodes that are immediate children of the {@code Element} node.
getAttribute(name)AttributeGets the attribute for thisElement node with the given name and no namespace.
getAttribute(name, namespace)AttributeGets the attribute for thisElement node with the given name and namespace.
getAttributes()Attribute[]Gets all attributes for thisElement node, in the order they appear in the document.
getChild(name)ElementGets the firstElement node with the given name and no namespace that is an immediatechild of thisElement node.
getChild(name, namespace)ElementGets the firstElement node with the given name and namespace that is an immediatechild of thisElement node.
getChildText(name)StringGets the text value of the node with the given name and no namespace, if the node is animmediate child of theElement node.
getChildText(name, namespace)StringGets the text value of the node with the given name and namespace, if the node is an immediatechild of theElement node.
getChildren()Element[]Gets allElement nodes that are immediate children of thisElement node, in theorder they appear in the document.
getChildren(name)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.
getChildren(name, namespace)Element[]Gets allElement nodes with the given name and namespace that are immediate children ofthisElement node, in the order they appear in the document.
getContent(index)ContentGets 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 theyappear in the document.
getDocument()DocumentGets the XML document that contains the {@code Element} node.
getName()StringGets the local name of theElement node.
getNamespace()NamespaceGets the namespace for theElement node.
getNamespace(prefix)NamespaceGets the namespace with the given prefix for theElement node.
getParentElement()ElementGets the node's parentElement node.
getQualifiedName()StringGets the local name and namespace prefix of theElement node, in the form[namespacePrefix]:[localName].
getText()StringGets the text value of theElement node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
isAncestorOf(other)BooleanDetermines whether thisElement node is a direct or indirect parent of a givenElement node.
isRootElement()BooleanDetermines whether theElement node is the document's root node.
removeAttribute(attribute)BooleanRemoves the given attribute for thisElement node, if such an attribute exists.
removeAttribute(attributeName)BooleanRemoves the attribute for thisElement node with the given name and no namespace, ifsuch an attribute exists.
removeAttribute(attributeName, namespace)BooleanRemoves the attribute for thisElement node with the given name and namespace, if suchan 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)ContentRemoves 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 thisElement node.
setAttribute(name, value)ElementSets the attribute for thisElement node with the given name, value, and no namespace.
setAttribute(name, value, namespace)ElementSets the attribute for thisElement node with the given name, value, and namespace.
setName(name)ElementSets the local name of theElement node.
setNamespace(namespace)ElementSets the namespace for theElement node.
setText(text)ElementSets the text value of theElement node.

EntityRef

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parentElement node.
getName()StringGets the name of theEntityReference node.
getParentElement()ElementGets the node's parentElement node.
getPublicId()StringGets the public ID of theEntityReference node.
getSystemId()StringGets the system ID of theEntityReference node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
setName(name)EntityRefSets the name of theEntityReference node.
setPublicId(id)EntityRefSets the public ID of theEntityReference node.
setSystemId(id)EntityRefSets the system ID of theEntityReference node.

Format

Methods

MethodReturn typeBrief description
format(document)StringOutputs the givenDocument as a formatted string.
format(element)StringOutputs the givenElement node as a formatted string.
setEncoding(encoding)FormatSets the character encoding that the formatter should use.
setIndent(indent)FormatSets the string used to indent child nodes relative to their parents.
setLineSeparator(separator)FormatSets the string to insert whenever the formatter would normally insert a line break.
setOmitDeclaration(omitDeclaration)FormatSets whether the formatter should omit the XML declaration, such as<?xml version="1.0"encoding="UTF-8"?>.
setOmitEncoding(omitEncoding)FormatSets 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

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

ProcessingInstruction

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parentElement node.
getData()StringGets the raw data for every instruction in theProcessingInstruction node.
getParentElement()ElementGets the node's parentElement node.
getTarget()StringGets the target for theProcessingInstruction node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.

Text

Methods

MethodReturn typeBrief description
append(text)TextAppends the given text to any content that already exists in the node.
detach()ContentDetaches the node from its parentElement node.
getParentElement()ElementGets the node's parentElement node.
getText()StringGets the text value of theText node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
setText(text)TextSets the text value of theText node.

XmlService

Properties

PropertyTypeDescription
ContentTypesContentTypeAn enumeration representing the types of XML content nodes.

Methods

MethodReturn typeBrief description
createCdata(text)CdataCreates an unattachedCDATASection node with the given value.
createComment(text)CommentCreates an unattachedComment node with the given value.
createDocType(elementName)DocTypeCreates an unattachedDocumentType node for the rootElement nodewith the given name.
createDocType(elementName, systemId)DocTypeCreates an unattachedDocumentType node for the rootElement nodewith the given name, and the given system ID for the external subset data.
createDocType(elementName, publicId, systemId)DocTypeCreates an unattachedDocumentType node for the rootElement nodewith the given name, and the given public ID and system ID for the external subset data.
createDocument()DocumentCreates an empty XML document.
createDocument(rootElement)DocumentCreates an XML document with the given rootElement node.
createElement(name)ElementCreates an unattachedElement node with the given local name and no namespace.
createElement(name, namespace)ElementCreates an unattachedElement node with the given local name and namespace.
createText(text)TextCreates an unattachedText node with the given value.
getCompactFormat()FormatCreates aFormat object for outputting a compact XML document.
getNamespace(uri)NamespaceCreates aNamespace with the given URI.
getNamespace(prefix, uri)NamespaceCreates aNamespace with the given prefix and URI.
getNoNamespace()NamespaceCreates aNamespace that represents the absence of a real namespace.
getPrettyFormat()FormatCreates aFormat object for outputting a human-readable XML document.
getRawFormat()FormatCreates aFormat object for outputting a raw XML document.
getXmlNamespace()NamespaceCreates aNamespace with the standardxml prefix.
parse(xml)DocumentCreates 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.