티스토리 수익 글 보기
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 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
| Name | Brief description |
|---|---|
Attribute | A representation of an XML attribute. |
Cdata | A representation of an XML CDATASection node. |
Comment | A representation of an XML Comment node. |
Content | A representation of a generic XML node. |
Content | An enumeration representing the types of XML content nodes. |
Doc | A representation of an XML Document node. |
Document | A representation of an XML document. |
Element | A representation of an XML Element node. |
Entity | A representation of an XML Entity node. |
Format | A formatter for outputting an XML document, with three pre-defined formats that can be further customized. |
Namespace | A representation of an XML namespace. |
Processing | A representation of an XML Processing node. |
Text | A representation of an XML Text 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|null | 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|null | Detaches the node from its parent Element node. |
get | Element|null | Gets the node’s parent Element node. |
get | String | Gets the text value of the Text node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. |
set | Text | Sets the text value of the Text node. |
Comment
Methods
| Method | Return type | Brief description |
|---|---|---|
detach() | Content|null | Detaches the node from its parent Element node. |
get | Element|null | Gets the node’s parent Element node. |
get | String | Gets the text value of the Comment node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. |
set | Comment | Sets the text value of the Comment node. |
Content
Methods
| Method | Return type | Brief description |
|---|---|---|
as | Cdata|null | Casts the node as a CDATASection node for the purposes of autocomplete. |
as | Comment|null | Casts the node as a Comment node for the purposes of autocomplete. |
as | Doc | Casts the node as a Document node for the purposes of autocomplete. |
as | Element|null | Casts the node as an Element node for the purposes of autocomplete. |
as | Entity | Casts the node as a Entity node for the purposes of autocomplete. |
as | Processing | Casts the node as a Processing node for the purposes of autocomplete. |
as | Text|null | Casts the node as a Text node for the purposes of autocomplete. |
detach() | Content|null | Detaches the node from its parent Element node. |
get | Element|null | Gets the node’s parent Element 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 order they appear in the document. |
ContentTypes
Properties
| Property | Type | Description |
|---|---|---|
CDATA | Enum | An XML CDATASection node. |
COMMENT | Enum | An XML Comment node. |
DOCTYPE | Enum | An XML Document node. |
ELEMENT | Enum | An XML Element node. |
ENTITYREF | Enum | An XML Entity node. |
PROCESSINGINSTRUCTION | Enum | An XML Processing node. |
TEXT | Enum | An XML Text node. |
DocType
Methods
| Method | Return type | Brief description |
|---|---|---|
detach() | Content|null | Detaches the node from its parent Element node. |
get | String | Gets the name of the root Element node specified in the Doc declaration. |
get | String | Gets the internal subset data for the Document node. |
get | Element|null | Gets the node’s parent Element node. |
get | String | Gets the public ID of the external subset data for the Document node. |
get | String | Gets the system ID of the external subset data for the Document node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. |
set | Doc | Sets the name of the root Element node to specify in the Doc
declaration. |
set | Doc | Sets the internal subset data for the Document node. |
set | Doc | Sets the public ID of the external subset data for the Document node. |
set | Doc | Sets the system ID of the external subset data for the Document 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 the document. |
clone | Content[] | Creates unattached copies of all nodes that are immediate children of the document. |
detach | Element|null | Detaches and returns the document’s root Element node. |
get | Content[] | Gets all nodes that are immediate children of the document. |
get | Content|null | Gets the node at the given index among all nodes that are immediate children of the document. |
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 they appear in the document. |
get | Doc | Gets the document’s Doc declaration. |
get | Element|null | Gets the document’s root Element node. |
has | Boolean | Determines whether the document has a root Element 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|null | Removes the node at the given index among all nodes that are immediate children of the document. |
set | Document | Sets the document’s Doc declaration. |
set | Document | Sets the document’s root Element node. |
Element
Methods
| Method | Return type | Brief description |
|---|---|---|
add | Element | Appends the given node as the last child of the Element node. |
add | Element | Inserts the given node at the given index among all nodes that are immediate children of the
Element node. |
clone | Content[] | Creates unattached copies of all nodes that are immediate children of the {@code Element} node. |
detach() | Content|null | Detaches the node from its parent Element node. |
get | Content[] | Gets all nodes that are immediate children of the {@code Element} node. |
get | Attribute|null | Gets the attribute for this Element node with the given name and no namespace. |
get | Attribute|null | Gets the attribute for this Element node with the given name and namespace. |
get | Attribute[] | Gets all attributes for this Element node, in the order they appear in the document. |
get | Element|null | Gets the first Element node with the given name and no namespace that is an immediate
child of this Element node. |
get | Element|null | Gets the first Element node with the given name and namespace that is an immediate
child of this Element node. |
get | String|null | Gets the text value of the node with the given name and no namespace, if the node is an
immediate child of the Element node. |
get | String|null | Gets the text value of the node with the given name and namespace, if the node is an immediate
child of the Element node. |
get | Element[] | Gets all Element nodes that are immediate children of this Element node, in the
order they appear in the document. |
get | 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. |
get | 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. |
get | Content|null | 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 they appear in the document. |
get | Document | Gets the XML document that contains the {@code Element} node. |
get | String | Gets the local name of the Element node. |
get | Namespace | Gets the namespace for the Element node. |
get | Namespace | Gets the namespace with the given prefix for the Element node. |
get | Element|null | Gets the node’s parent Element node. |
get | String | Gets the local name and namespace prefix of the Element node, in the form [namespacePrefix]:[localName]. |
get | String | Gets the text value of the Element node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. |
is | Boolean | Determines whether this Element node is a direct or indirect parent of a given Element node. |
is | Boolean | Determines whether the Element node is the document’s root node. |
remove | Boolean | Removes the given attribute for this Element node, if such an attribute exists. |
remove | Boolean | Removes the attribute for this Element node with the given name and no namespace, if
such an attribute exists. |
remove | Boolean | Removes the attribute for this Element node with the given name and namespace, if such
an 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|null | 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 this Element node. |
set | Element | Sets the attribute for this Element node with the given name, value, and no namespace. |
set | Element | Sets the attribute for this Element node with the given name, value, and namespace. |
set | Element | Sets the local name of the Element node. |
set | Element | Sets the namespace for the Element node. |
set | Element | Sets the text value of the Element node. |
EntityRef
Methods
| Method | Return type | Brief description |
|---|---|---|
detach() | Content|null | Detaches the node from its parent Element node. |
get | String | Gets the name of the Entity node. |
get | Element|null | Gets the node’s parent Element node. |
get | String|null | Gets the public ID of the Entity node. |
get | String|null | Gets the system ID of the Entity node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. |
set | Entity | Sets the name of the Entity node. |
set | Entity | Sets the public ID of the Entity node. |
set | Entity | Sets the system ID of the Entity node. |
Format
Methods
| Method | Return type | Brief description |
|---|---|---|
format(document) | String | Outputs the given Document as a formatted string. |
format(element) | String | Outputs the given Element 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 the
encoding 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|null | Detaches the node from its parent Element node. |
get | String | Gets the raw data for every instruction in the Processing node. |
get | Element|null | Gets the node’s parent Element node. |
get | String | Gets the target for the Processing node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they 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|null | Detaches the node from its parent Element node. |
get | Element|null | Gets the node’s parent Element node. |
get | String | Gets the text value of the Text node. |
get | String | Gets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document. |
set | Text | Sets the text value of the Text 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 unattached CDATASection node with the given value. |
create | Comment | Creates an unattached Comment node with the given value. |
create | Doc | Creates an unattached Document node for the root Element node
with the given name. |
create | Doc | Creates an unattached Document node for the root Element node
with the given name, and the given system ID for the external subset data. |
create | Doc | Creates an unattached Document node for the root Element node
with 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 root Element node. |
create | Element | Creates an unattached Element node with the given local name and no namespace. |
create | Element | Creates an unattached Element node with the given local name and namespace. |
create | Text | Creates an unattached Text node with the given value. |
get | Format | Creates a Format object for outputting a compact XML document. |
get | Namespace | Creates a Namespace with the given URI. |
get | Namespace | Creates a Namespace with the given prefix and URI. |
get | Namespace | Creates a Namespace that represents the absence of a real namespace. |
get | Format | Creates a Format object for outputting a human-readable XML document. |
get | Format | Creates a Format object for outputting a raw XML document. |
get | Namespace | Creates a Namespace with the standard xml prefix. |
parse(xml) | Document | Creates an Document from the given XML, without validating the XML. |
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-06-22 UTC.