Stay organized with collections Save and categorize content based on your preferences.
XmlServiceThis 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. function parseXml() { const url = 'https://gsuite-developers.googleblog.com/atom.xml'; const xml = UrlFetchApp.fetch(url).getContentText(); const document = XmlService.parse(xml); const root = document.getRootElement(); const atom = XmlService.getNamespace('http://www.w3.org/2005/Atom'); const entries = root.getChildren('entry', atom); for (let i = 0; i < entries.length; i++) { const title = entries[i].getChild('title', atom).getText(); const categoryElements = entries[i].getChildren('category', atom); const labels = []; for (let j = 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. function createXml() { const root = XmlService.createElement('threads'); const threads = GmailApp.getInboxThreads(); for (let i = 0; i < threads.length; i++) { const child = XmlService.createElement('thread') .setAttribute('messageCount', threads[i].getMessageCount()) .setAttribute('isUnread', threads[i].isUnread()) .setText(threads[i].getFirstMessageSubject()); root.addContent(child); } const document = XmlService.createDocument(root); const xml = XmlService.getPrettyFormat().format(document); Logger.log(xml); }Properties Property Type Description
ContentTypes
ContentType
An enumeration representing the types of XML content nodes. Methods Detailed documentation createCdata(text)
Creates an unattached CDATASection
node with the given value.
text
String
the value to set Return
Cdata
— the newly created CDATASection
node
createDocType(elementName)
Creates an unattached DocumentType
node for the root Element
node with the given name.
elementName
String
the name of the root Element
node to specify in the DocType
declaration Return
DocType
— the newly created DocumentType
node
createDocType(elementName, systemId)
Creates an unattached DocumentType
node for the root Element
node with the given name, and the given system ID for the external subset data.
elementName
String
the name of the root Element
node to specify in the DocType
declaration systemId
String
the system ID of the external subset data to set Return
DocType
— the newly created DocumentType
node
createDocType(elementName, publicId, systemId)
Creates an unattached DocumentType
node for the root Element
node with the given name, and the given public ID and system ID for the external subset data.
elementName
String
the name of the root Element
node to specify in the DocType
declaration publicId
String
the public ID of the external subset data to set systemId
String
the system ID of the external subset data to set Return
DocType
— the newly created DocumentType
node
createDocument()
Creates an empty XML document.
ReturnDocument
— the newly created document
createDocument(rootElement)
Creates an XML document with the given root Element
node.
rootElement
Element
the root Element
node to set Return
Document
— the newly created document
createElement(name)
Creates an unattached Element
node with the given local name and no namespace.
name
String
the local name to set Return
Element
— the newly created Element
node
createElement(name, namespace)
Creates an unattached Element
node with the given local name and namespace.
name
String
the local name to set namespace
Namespace
the namespace to set Return
Element
— the newly created Element
node
createText(text)
Creates an unattached Text
node with the given value.
text
String
the value to set Return
Text
— the newly created Text
node
getCompactFormat()
Creates a Format
object for outputting a compact XML document. The formatter defaults to UTF-8
encoding, no indentation, and no additional line breaks, but includes the XML declaration and its encoding.
// Log an XML document in compact form. const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const document = XmlService.parse(xml); const output = XmlService.getCompactFormat().format(document); Logger.log(output);Return
Format
— the newly created formatter
getNamespace(uri)
Creates a Namespace
with the given URI.
uri
String
the URI for the namespace Return
Namespace
— the newly created namespace
getNamespace(prefix, uri)
Creates a Namespace
with the given prefix and URI.
prefix
String
the prefix for the namespace uri
String
the URI for the namespace Return
Namespace
— the newly created namespace
getNoNamespace()
Creates a Namespace
that represents the absence of a real namespace.
Namespace
— the newly created namespace
getPrettyFormat()
Creates a Format
object for outputting a human-readable XML document. The formatter defaults to UTF-8
encoding, two-space indentation, \r\n
line separators after every node, and includes the XML declaration and its encoding.
// Log an XML document in human-readable form. const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const document = XmlService.parse(xml); const output = XmlService.getPrettyFormat().format(document); Logger.log(output);Return
Format
— the newly created formatter
getRawFormat()
Creates a Format
object for outputting a raw XML document. The formatter defaults to UTF-8
encoding, no indentation and no line breaks other than those provided in the XML document itself, and includes the XML declaration and its encoding.
// Log an XML document in raw form. const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const document = XmlService.parse(xml); const output = XmlService.getRawFormat().format(document); Logger.log(output);Return
Format
— the newly created formatter
getXmlNamespace()
Creates a Namespace
with the standard xml
prefix.
Namespace
— the newly created namespace
parse(xml)
Creates an Document
from the given XML, without validating the XML.
const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const doc = XmlService.parse(xml);Parameters Name Type Description
xml
String
the XML to parse Return
Document
— the newly created document
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 2024-12-02 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-02 UTC."],[[["The XmlService in Apps Script enables parsing, navigation, and creation of XML documents programmatically."],["It offers methods to create various XML node types like elements, attributes, text nodes, and comments."],["The service provides functionality to format XML output in compact, pretty, or raw forms."],["`XmlService.parse()` can be used to create a Document object from an XML string, while methods like `createElement()` and `setAttribute()` help build new XML structures."],["Developers can use this service to interact with external XML data or generate XML for various purposes within their Apps Script projects."]]],["The XmlService enables parsing and creating XML documents. Key actions include: parsing XML strings into a navigable document via `parse()`; creating XML elements with `createElement()`; adding content to elements; defining namespaces with `getNamespace()`; formatting XML output using `getCompactFormat()`, `getPrettyFormat()`, or `getRawFormat()`. The service also provides methods to create various types of nodes such as `Cdata`, `Comment`, `DocType`, and `Text`, allowing for detailed XML structure manipulation.\n"]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4