Class XmlService

  • The XmlService allows scripts to parse, navigate, and programmatically create XML documents.

  • It provides methods to create various XML nodes such as CDATA, Comments, DocTypes, Elements, and Text.

  • You can create empty XML documents or documents with a specified root element.

  • The service offers different formatting options (Compact, Pretty, Raw) for outputting XML documents.

  • You can parse existing XML strings into a Document object for manipulation.

XmlService

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 the// Google Workspace Developer blog.functionparseXml(){consturl='https://gsuite-developers.googleblog.com/atom.xml';constxml=UrlFetchApp.fetch(url).getContentText();constdocument=XmlService.parse(xml);constroot=document.getRootElement();constatom=XmlService.getNamespace('http://www.w3.org/2005/Atom');constentries=root.getChildren('entry',atom);for(leti=0;i <entries.length;i++){consttitle=entries[i].getChild('title',atom).getText();constcategoryElements=entries[i].getChildren('category',atom);constlabels=[];for(letj=0;j <categoryElements.length;j++){labels.push(categoryElements[j].getAttribute('term').getValue());}Logger.log('%s (%s)',title,labels.join(', '));}}// Create and log an XML representation of the threads in your Gmail inbox.functioncreateXml(){constroot=XmlService.createElement('threads');constthreads=GmailApp.getInboxThreads();for(leti=0;i <threads.length;i++){constchild=XmlService.createElement('thread').setAttribute('messageCount',threads[i].getMessageCount()).setAttribute('isUnread',threads[i].isUnread()).setText(threads[i].getFirstMessageSubject());root.addContent(child);}constdocument=XmlService.createDocument(root);constxml=XmlService.getPrettyFormat().format(document);Logger.log(xml);}

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.

Detailed documentation

createCdata(text)

Creates an unattachedCDATASection node with the given value.

Parameters

NameTypeDescription
textStringthe value to set

Return

Cdata — the newly createdCDATASection node


createComment(text)

Creates an unattachedComment node with the given value.

Parameters

NameTypeDescription
textStringthe value to set

Return

Comment — the newly createdComment node


createDocType(elementName)

Creates an unattachedDocumentType node for the rootElement nodewith the given name.

Parameters

NameTypeDescription
elementNameStringthe name of the rootElement node to specify in theDocType declaration

Return

DocType — the newly createdDocumentType node


createDocType(elementName, systemId)

Creates an unattachedDocumentType node for the rootElement nodewith the given name, and the given system ID for the external subset data.

Parameters

NameTypeDescription
elementNameStringthe name of the rootElement node to specify in theDocType declaration
systemIdStringthe system ID of the external subset data to set

Return

DocType — the newly createdDocumentType node


createDocType(elementName, publicId, systemId)

Creates an unattachedDocumentType node for the rootElement nodewith the given name, and the given public ID and system ID for the external subset data.

Parameters

NameTypeDescription
elementNameStringthe name of the rootElement node to specify in theDocType declaration
publicIdStringthe public ID of the external subset data to set
systemIdStringthe system ID of the external subset data to set

Return

DocType — the newly createdDocumentType node


createDocument()

Creates an empty XML document.

Return

Document — the newly created document


createDocument(rootElement)

Creates an XML document with the given rootElement node.

Parameters

NameTypeDescription
rootElementElementthe rootElement node to set

Return

Document — the newly created document


createElement(name)

Creates an unattachedElement node with the given local name and no namespace.

Parameters

NameTypeDescription
nameStringthe local name to set

Return

Element — the newly createdElement node


createElement(name, namespace)

Creates an unattachedElement node with the given local name and namespace.

Parameters

NameTypeDescription
nameStringthe local name to set
namespaceNamespacethe namespace to set

Return

Element — the newly createdElement node


createText(text)

Creates an unattachedText node with the given value.

Parameters

NameTypeDescription
textStringthe value to set

Return

Text — the newly createdText node


getCompactFormat()

Creates aFormat object for outputting a compact XML document. The formatterdefaults toUTF-8 encoding, no indentation, and no additional line breaks, but includesthe XML declaration and its encoding.

// Log an XML document in compact form.constxml='<root><a><b>Text!</b><b>More text!</b></a></root>';constdocument=XmlService.parse(xml);constoutput=XmlService.getCompactFormat().format(document);Logger.log(output);

Return

Format — the newly created formatter


getNamespace(uri)

Creates aNamespace with the given URI.

Parameters

NameTypeDescription
uriStringthe URI for the namespace

Return

Namespace — the newly created namespace


getNamespace(prefix, uri)

Creates aNamespace with the given prefix and URI.

Parameters

NameTypeDescription
prefixStringthe prefix for the namespace
uriStringthe URI for the namespace

Return

Namespace — the newly created namespace


getNoNamespace()

Creates aNamespace that represents the absence of a real namespace.

Return

Namespace — the newly created namespace


getPrettyFormat()

Creates aFormat object for outputting a human-readable XML document. The formatterdefaults toUTF-8 encoding, two-space indentation,\r\n line separators afterevery node, and includes the XML declaration and its encoding.

// Log an XML document in human-readable form.constxml='<root><a><b>Text!</b><b>More text!</b></a></root>';constdocument=XmlService.parse(xml);constoutput=XmlService.getPrettyFormat().format(document);Logger.log(output);

Return

Format — the newly created formatter


getRawFormat()

Creates aFormat object for outputting a raw XML document. The formatter defaults toUTF-8 encoding, no indentation and no line breaks other than those provided in the XMLdocument itself, and includes the XML declaration and its encoding.

// Log an XML document in raw form.constxml='<root><a><b>Text!</b><b>More text!</b></a></root>';constdocument=XmlService.parse(xml);constoutput=XmlService.getRawFormat().format(document);Logger.log(output);

Return

Format — the newly created formatter


getXmlNamespace()

Creates aNamespace with the standardxml prefix.

Return

Namespace — the newly created namespace


parse(xml)

Creates anDocument from the given XML, without validating the XML.

constxml='<root><a><b>Text!</b><b>More text!</b></a></root>';constdoc=XmlService.parse(xml);

Parameters

NameTypeDescription
xmlStringthe XML to parse

Return

Document — the newly created document

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.