此服务允许脚本解析、导航和以编程方式创建 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 内容节点类型的枚举。 |