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 create XML 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 10
threads.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
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.
ContentType An enumeration representing the types of XML content nodes.
DocType A representation of an XML DocumentType node.
Document A representation of an XML document.
Element A representation of an XML Element node.
EntityRef A representation of an XML EntityReference 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.
ProcessingInstruction A representation of an XML ProcessingInstruction node.
Text A representation of an XML Text node.
XmlService This 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() Namespace Gets the namespace for the attribute.
getValue() StringGets the value of the attribute.
setName(name) Attribute Sets the local name of the attribute.
setNamespace(namespace) Attribute Sets the namespace for the attribute.
setValue(value) Attribute Sets the value of the attribute.

Cdata

Methods

MethodReturn typeBrief description
append(text) Text Appends the given text to any content that already exists in the node.
detach() Content Detaches the node from its parent Element node.
getParentElement() Element Gets the node's parent Element node.
getText() StringGets the text value of the Text 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.
setText(text) Text Sets the text value of the Text node.

Comment

Methods

MethodReturn typeBrief description
detach() Content Detaches the node from its parent Element node.
getParentElement() Element Gets the node's parent Element node.
getText() StringGets the text value of the Comment 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.
setText(text) Comment Sets the text value of the Comment node.

Content

Methods

MethodReturn typeBrief description
asCdata() Cdata Casts the node as a CDATASection node for the purposes of autocomplete.
asComment() Comment Casts the node as a Comment node for the purposes of autocomplete.
asDocType() DocType Casts the node as a DocumentType node for the purposes of autocomplete.
asElement() Element Casts the node as an Element node for the purposes of autocomplete.
asEntityRef() EntityRef Casts the node as a EntityReference node for the purposes of autocomplete.
asProcessingInstruction() ProcessingInstruction Casts the node as a ProcessingInstruction node for the purposes of autocomplete.
asText() Text Casts the node as a Text node for the purposes of autocomplete.
detach() Content Detaches the node from its parent Element node.
getParentElement() Element Gets the node's parent Element node.
getType() ContentType Gets the node's content type.
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.

ContentType

Properties

PropertyTypeDescription
CDATAEnumAn XML CDATASection node.
COMMENTEnumAn XML Comment node.
DOCTYPEEnumAn XML DocumentType node.
ELEMENTEnumAn XML Element node.
ENTITYREFEnumAn XML EntityReference node.
PROCESSINGINSTRUCTIONEnumAn XML ProcessingInstruction node.
TEXTEnumAn XML Text node.

DocType

Methods

MethodReturn typeBrief description
detach() Content Detaches the node from its parent Element node.
getElementName() StringGets the name of the root Element node specified in the DocType declaration.
getInternalSubset() StringGets the internal subset data for the DocumentType node.
getParentElement() Element Gets the node's parent Element node.
getPublicId() StringGets the public ID of the external subset data for the DocumentType node.
getSystemId() StringGets the system ID of the external subset data for the DocumentType 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.
setElementName(name) DocType Sets the name of the root Element node to specify in the DocType declaration.
setInternalSubset(data) DocType Sets the internal subset data for the DocumentType node.
setPublicId(id) DocType Sets the public ID of the external subset data for the DocumentType node.
setSystemId(id) DocType Sets the system ID of the external subset data for the DocumentType node.

Document

Methods

MethodReturn typeBrief description
addContent(content) Document Appends the given node to the end of the document.
addContent(index, content) Document Inserts the given node at the given index among all nodes that are immediate children of the document.
cloneContent() Content[] Creates unattached copies of all nodes that are immediate children of the document.
detachRootElement() Element Detaches and returns the document's root Element node.
getAllContent() Content[] Gets all nodes that are immediate children of the document.
getContent(index) Content Gets the node at the given index among all nodes that are immediate children of the document.
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 they appear in the document.
getDocType() DocType Gets the document's DocType declaration.
getRootElement() Element Gets the document's root Element node.
hasRootElement() BooleanDetermines whether the document has a root Element 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) Content Removes the node at the given index among all nodes that are immediate children of the document.
setDocType(docType) Document Sets the document's DocType declaration.
setRootElement(element) Document Sets the document's root Element node.

Element

Methods

MethodReturn typeBrief description
addContent(content) Element Appends the given node as the last child of the Element node.
addContent(index, content) Element Inserts 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 Detaches the node from its parent Element node.
getAllContent() Content[] Gets all nodes that are immediate children of the {@code Element} node.
getAttribute(name) Attribute Gets the attribute for this Element node with the given name and no namespace.
getAttribute(name, namespace) Attribute Gets 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 Gets the first Element node with the given name and no namespace that is an immediate child of this Element node.
getChild(name, namespace) Element Gets the first Element node with the given name and namespace that is an immediate child of this Element node.
getChildText(name) StringGets 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) StringGets 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 Gets 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() Document Gets the XML document that contains the {@code Element} node.
getName() StringGets the local name of the Element node.
getNamespace() Namespace Gets the namespace for the Element node.
getNamespace(prefix) Namespace Gets the namespace with the given prefix for the Element node.
getParentElement() Element Gets 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 Removes the node at the given index among all nodes that are immediate children of the {@code Element} node.
setAttribute(attribute) Element Sets the given attribute for this Element node.
setAttribute(name, value) Element Sets the attribute for this Element node with the given name, value, and no namespace.
setAttribute(name, value, namespace) Element Sets the attribute for this Element node with the given name, value, and namespace.
setName(name) Element Sets the local name of the Element node.
setNamespace(namespace) Element Sets the namespace for the Element node.
setText(text) Element Sets the text value of the Element node.

EntityRef

Methods

MethodReturn typeBrief description
detach() Content Detaches the node from its parent Element node.
getName() StringGets the name of the EntityReference node.
getParentElement() Element Gets the node's parent Element node.
getPublicId() StringGets the public ID of the EntityReference node.
getSystemId() StringGets the system ID of the EntityReference 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.
setName(name) EntityRef Sets the name of the EntityReference node.
setPublicId(id) EntityRef Sets the public ID of the EntityReference node.
setSystemId(id) EntityRef Sets the system ID of the EntityReference node.

Format

Methods

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

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

ProcessingInstruction

Methods

MethodReturn typeBrief description
detach() Content Detaches the node from its parent Element node.
getData() StringGets the raw data for every instruction in the ProcessingInstruction node.
getParentElement() Element Gets 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

Methods

MethodReturn typeBrief description
append(text) Text Appends the given text to any content that already exists in the node.
detach() Content Detaches the node from its parent Element node.
getParentElement() Element Gets the node's parent Element node.
getText() StringGets the text value of the Text 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.
setText(text) Text Sets the text value of the Text node.

XmlService

Properties

PropertyTypeDescription
ContentTypesContentType An enumeration representing the types of XML content nodes.

Methods

MethodReturn typeBrief description
createCdata(text) Cdata Creates an unattached CDATASection node with the given value.
createComment(text) Comment Creates an unattached Comment node with the given value.
createDocType(elementName) DocType Creates an unattached DocumentType node for the root Element node with the given name.
createDocType(elementName, systemId) DocType Creates an unattached DocumentType node for the root Element node with the given name, and the given system ID for the external subset data.
createDocType(elementName, publicId, systemId) DocType 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.
createDocument() Document Creates an empty XML document.
createDocument(rootElement) Document Creates an XML document with the given root Element node.
createElement(name) Element Creates an unattached Element node with the given local name and no namespace.
createElement(name, namespace) Element Creates an unattached Element node with the given local name and namespace.
createText(text) Text Creates an unattached Text node with the given value.
getCompactFormat() Format Creates a Format object for outputting a compact XML document.
getNamespace(uri) Namespace Creates a Namespace with the given URI.
getNamespace(prefix, uri) Namespace Creates a Namespace with the given prefix and URI.
getNoNamespace() Namespace Creates a Namespace that represents the absence of a real namespace.
getPrettyFormat() Format Creates a Format object for outputting a human-readable XML document.
getRawFormat() Format Creates a Format object for outputting a raw XML document.
getXmlNamespace() 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 2024年12月05日 UTC.