此服务允许脚本解析、导航和以编程方式创建 XML 文档。
// 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); }
类
名称 | 简介 |
---|---|
Attribute | XML 属性的表示法。 |
Cdata | XML CDATASection 节点的表示形式。 |
Comment | XML Comment 节点的表示形式。 |
Content | 通用 XML 节点的表示形式。 |
ContentType | 表示 XML 内容节点类型的枚举。 |
DocType | XML DocumentType 节点的表示形式。 |
Document | XML 文档的表示形式。 |
Element | XML Element 节点的表示形式。 |
EntityRef | XML EntityReference 节点的表示形式。 |
Format | 用于输出 XML 文档的格式设置工具,提供三种可以进一步自定义的预定义格式。 |
Namespace | XML 命名空间的表示形式。 |
ProcessingInstruction | XML ProcessingInstruction 节点的表示形式。 |
Text | XML Text 节点的表示形式。 |
XmlService | 此服务允许脚本解析、导航和以编程方式创建 XML 文档。 |
Attribute
方法
方法 | 返回类型 | 简介 |
---|---|---|
getName() | String | 获取属性的本地名称。 |
getNamespace() | Namespace | 获取属性的命名空间。 |
getValue() | String | 获取属性的值。 |
setName(name) | Attribute | 设置属性的本地名称。 |
setNamespace(namespace) | Attribute | 设置属性的命名空间。 |
setValue(value) | Attribute | 设置属性的值。 |
Cdata
方法
方法 | 返回类型 | 简介 |
---|---|---|
append(text) | Text | 将指定文本附加到节点中已存在的任何内容。 |
detach() | Content | 将节点与其父 Element 节点分离。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getText() | String | 获取 Text 节点的文本值。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
setText(text) | Text | 设置 Text 节点的文本值。 |
Comment
方法
方法 | 返回类型 | 简介 |
---|---|---|
detach() | Content | 将节点与其父 Element 节点分离。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getText() | String | 获取 Comment 节点的文本值。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
setText(text) | Comment | 设置 Comment 节点的文本值。 |
Content
方法
方法 | 返回类型 | 简介 |
---|---|---|
asCdata() | Cdata | 出于自动补全目的,将节点转换为 CDATASection 节点。 |
asComment() | Comment | 出于自动补全目的,将节点转换为 Comment 节点。 |
asDocType() | DocType | 出于自动补全目的,将节点转换为 DocumentType 节点。 |
asElement() | Element | 出于自动补全目的,将节点转换为 Element 节点。 |
asEntityRef() | EntityRef | 出于自动补全目的,将节点转换为 EntityReference 节点。 |
asProcessingInstruction() | ProcessingInstruction | 出于自动补全目的,将节点转换为 ProcessingInstruction 节点。 |
asText() | Text | 出于自动补全目的,将节点转换为 Text 节点。 |
detach() | Content | 将节点与其父 Element 节点分离。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getType() | ContentType | 获取节点的内容类型。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
ContentType
属性
媒体资源 | 类型 | 说明 |
---|---|---|
CDATA | Enum | XML CDATASection 节点。 |
COMMENT | Enum | XML Comment 节点。 |
DOCTYPE | Enum | XML DocumentType 节点。 |
ELEMENT | Enum | XML Element 节点。 |
ENTITYREF | Enum | XML EntityReference 节点。 |
PROCESSINGINSTRUCTION | Enum | XML ProcessingInstruction 节点。 |
TEXT | Enum | XML Text 节点。 |
DocType
方法
方法 | 返回类型 | 简介 |
---|---|---|
detach() | Content | 将节点与其父 Element 节点分离。 |
getElementName() | String | 获取 DocType 声明中指定的根 Element 节点的名称。 |
getInternalSubset() | String | 获取 DocumentType 节点的内部子集数据。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getPublicId() | String | 获取 DocumentType 节点的外部子集数据的公开 ID。 |
getSystemId() | String | 获取 DocumentType 节点的外部子集数据的系统 ID。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
setElementName(name) | DocType | 设置要在 DocType 声明中指定的根 Element 节点的名称。 |
setInternalSubset(data) | DocType | 为 DocumentType 节点设置内部子集数据。 |
setPublicId(id) | DocType | 设置 DocumentType 节点的外部子集数据的公开 ID。 |
setSystemId(id) | DocType | 设置 DocumentType 节点的外部子集数据的系统 ID。 |
Document
方法
方法 | 返回类型 | 简介 |
---|---|---|
addContent(content) | Document | 将指定节点附加到文档末尾。 |
addContent(index, content) | Document | 在文档直接子节点的所有节点中插入给定索引处的给定节点。 |
cloneContent() | Content[] | 创建作为文档的直接子节点的所有节点的未附加副本。 |
detachRootElement() | Element | 分离并返回文档的根 Element 节点。 |
getAllContent() | Content[] | 获取作为文档的直接子节点的所有节点。 |
getContent(index) | Content | 获取属于文档的直接子节点的所有节点中位于指定索引处的节点。 |
getContentSize() | Integer | 获取作为文档的直接子项的节点数。 |
getDescendants() | Content[] | 按照文档在文档中出现的顺序获取作为文档直接或间接子节点的所有节点。 |
getDocType() | DocType | 获取文档的 DocType 声明。 |
getRootElement() | Element | 获取文档的根 Element 节点。 |
hasRootElement() | Boolean | 确定文档是否具有根 Element 节点。 |
removeContent() | Content[] | 移除所有作为文档的直接子节点的节点。 |
removeContent(content) | Boolean | 删除指定节点(如果该节点是文档的直接子节点)。 |
removeContent(index) | Content | 从作为文档的直接子节点的所有节点中移除位于指定索引处的节点。 |
setDocType(docType) | Document | 设置文档的 DocType 声明。 |
setRootElement(element) | Document | 设置文档的根 Element 节点。 |
Element
方法
方法 | 返回类型 | 简介 |
---|---|---|
addContent(content) | Element | 将指定节点作为 Element 节点的最后一个子项附加。 |
addContent(index, content) | Element | 在属于 Element 节点直接子节点的所有节点中,在给定索引处插入给定节点。 |
cloneContent() | Content[] | 创建所有属于 {@code Element} 节点直接子节点的节点的未附加副本。 |
detach() | Content | 将节点与其父 Element 节点分离。 |
getAllContent() | Content[] | 获取属于 {@code Element} 节点的直接子节点的所有节点。 |
getAttribute(name) | Attribute | 获取具有指定名称但没有命名空间的该 Element 节点的属性。 |
getAttribute(name, namespace) | Attribute | 获取具有指定名称和命名空间的此 Element 节点的属性。 |
getAttributes() | Attribute[] | 按照属性在文档中出现的顺序获取此 Element 节点的所有属性。 |
getChild(name) | Element | 获取具有指定名称的第一个 Element 节点,但没有为该 Element 节点直接子项的命名空间。 |
getChild(name, namespace) | Element | 获取具有指定名称和命名空间的第一个 Element 节点,该节点是该 Element 节点的直接子节点。 |
getChildText(name) | String | 获取具有指定名称和无命名空间的节点的文本值(如果该节点是 Element 节点的直接子节点)。 |
getChildText(name, namespace) | String | 获取具有指定名称和命名空间的节点的文本值(如果该节点是 Element 节点的直接子节点)。 |
getChildren() | Element[] | 按照节点在文档中出现的顺序获取属于此Element 节点直接子节点的所有Element 节点。 |
getChildren(name) | Element[] | 按照这些节点在文档中出现的顺序,获取具有给定名称且没有命名空间的所有 Element 节点,这些节点是此 Element 节点的直接子节点。 |
getChildren(name, namespace) | Element[] | 获取具有指定名称和命名空间的所有 Element 节点,这些节点是此 Element 节点的直接子节点(按照它们在文档中出现的顺序)。 |
getContent(index) | Content | 从属于 {@code Element} 节点的直接子节点的所有节点中获取给定索引处的节点。 |
getContentSize() | Integer | 获取属于 {@code Element} 节点直接子节点的节点数。 |
getDescendants() | Content[] | 按照节点在文档中出现的顺序,获取作为 {@code Element} 节点的直接或间接子节点的所有节点。 |
getDocument() | Document | 获取包含 {@code Element} 节点的 XML 文档。 |
getName() | String | 获取 Element 节点的本地名称。 |
getNamespace() | Namespace | 获取 Element 节点的命名空间。 |
getNamespace(prefix) | Namespace | 获取具有 Element 节点给定前缀的命名空间。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getQualifiedName() | String | 获取 Element 节点的本地名称和命名空间前缀,格式为 [namespacePrefix]:[localName] 。 |
getText() | String | 获取 Element 节点的文本值。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
isAncestorOf(other) | Boolean | 确定此 Element 节点是给定 Element 节点的直接父节点还是间接父节点。 |
isRootElement() | Boolean | 确定 Element 节点是否为文档的根节点。 |
removeAttribute(attribute) | Boolean | 移除此 Element 节点的指定属性(如果存在)。 |
removeAttribute(attributeName) | Boolean | 移除具有指定名称和无命名空间的此 Element 节点的属性(如果存在此类属性)。 |
removeAttribute(attributeName, namespace) | Boolean | 移除具有给定名称和命名空间的此 Element 节点的属性(如果存在此类属性)。 |
removeContent() | Content[] | 移除所有属于 {@code Element} 节点直接子节点的节点。 |
removeContent(content) | Boolean | 移除指定节点(如果该节点是 {@code Element} 节点的直接子节点)。 |
removeContent(index) | Content | 从属于 {@code Element} 节点的直接子节点的所有节点中移除给定索引处的节点。 |
setAttribute(attribute) | Element | 设置此 Element 节点的给定属性。 |
setAttribute(name, value) | Element | 使用此 Element 节点的指定名称、值设置属性,但不指定命名空间。 |
setAttribute(name, value, namespace) | Element | 使用给定的名称、值和命名空间设置此 Element 节点的属性。 |
setName(name) | Element | 设置 Element 节点的本地名称。 |
setNamespace(namespace) | Element | 设置 Element 节点的命名空间。 |
setText(text) | Element | 设置 Element 节点的文本值。 |
EntityRef
方法
方法 | 返回类型 | 简介 |
---|---|---|
detach() | Content | 将节点与其父 Element 节点分离。 |
getName() | String | 获取 EntityReference 节点的名称。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getPublicId() | String | 获取 EntityReference 节点的公共 ID。 |
getSystemId() | String | 获取 EntityReference 节点的系统 ID。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
setName(name) | EntityRef | 设置 EntityReference 节点的名称。 |
setPublicId(id) | EntityRef | 设置 EntityReference 节点的公共 ID。 |
setSystemId(id) | EntityRef | 设置 EntityReference 节点的系统 ID。 |
Format
方法
方法 | 返回类型 | 简介 |
---|---|---|
format(document) | String | 以格式化字符串的形式输出指定的 Document 。 |
format(element) | String | 以格式化字符串形式输出指定的 Element 节点。 |
setEncoding(encoding) | Format | 设置格式设置工具应使用的字符编码。 |
setIndent(indent) | Format | 设置用于相对于子节点缩进的字符串。 |
setLineSeparator(separator) | Format | 设置每当格式设置工具正常插入换行符时要插入的字符串。 |
setOmitDeclaration(omitDeclaration) | Format | 设置格式化程序是否应省略 XML 声明,例如 <?xml version="1.0"
encoding="UTF-8"?> 。 |
setOmitEncoding(omitEncoding) | Format | 设置格式设置工具是否应在 XML 声明(例如 <?xml version="1.0" encoding="UTF-8"?> 中的编码字段)中省略编码。 |
Namespace
方法
方法 | 返回类型 | 简介 |
---|---|---|
getPrefix() | String | 获取命名空间的前缀。 |
getURI() | String | 获取命名空间的 URI。 |
ProcessingInstruction
方法
方法 | 返回类型 | 简介 |
---|---|---|
detach() | Content | 将节点与其父 Element 节点分离。 |
getData() | String | 获取 ProcessingInstruction 节点中每条指令的原始数据。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getTarget() | String | 获取 ProcessingInstruction 节点的目标。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
Text
方法
方法 | 返回类型 | 简介 |
---|---|---|
append(text) | Text | 将指定文本附加到节点中已存在的任何内容。 |
detach() | Content | 将节点与其父 Element 节点分离。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getText() | String | 获取 Text 节点的文本值。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
setText(text) | Text | 设置 Text 节点的文本值。 |
XmlService
属性
媒体资源 | 类型 | 说明 |
---|---|---|
ContentTypes | ContentType | 表示 XML 内容节点类型的枚举。 |