Class XmlService

XmlService

借助此服务,脚本可以解析、浏览和以程序化方式创建 XML 文档。

// 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); }

属性

属性类型说明
ContentTypesContentType表示 XML 内容节点类型的枚举。

方法

方法返回类型简介
createCdata(text)Cdata创建具有给定值的未附加 CDATASection 节点。
createComment(text)Comment创建具有给定值的未附加 Comment 节点。
createDocType(elementName)DocType为根 Element 节点创建一个名为给定名称的未附加 DocumentType 节点。
createDocType(elementName, systemId)DocType为根 Element 节点创建一个未附加的 DocumentType 节点,并为外部子集数据提供给定的名称和系统 ID。
createDocType(elementName, publicId, systemId)DocType为根 Element 节点创建一个未附加的 DocumentType 节点,并为外部子集数据提供给定的名称、公共 ID 和系统 ID。
createDocument()Document创建一个空的 XML 文档。
createDocument(rootElement)Document使用给定的根 Element 节点创建 XML 文档。
createElement(name)Element创建具有给定本地名称且没有命名空间的未附加 Element 节点。
createElement(name, namespace)Element使用指定的本地名称和命名空间创建未附加的 Element 节点。
createText(text)Text创建具有给定值的未附加 Text 节点。
getCompactFormat()Format创建 Format 对象以输出紧凑的 XML 文档。
getNamespace(uri)Namespace使用给定 URI 创建 Namespace
getNamespace(prefix, uri)Namespace使用给定的前缀和 URI 创建 Namespace
getNoNamespace()Namespace创建一个 Namespace,表示不存在实际命名空间。
getPrettyFormat()Format创建 Format 对象以输出可读性良好的 XML 文档。
getRawFormat()Format创建 Format 对象以输出原始 XML 文档。
getXmlNamespace()Namespace创建使用标准 xml 前缀的 Namespace
parse(xml)Document使用给定 XML 创建 Document,但不会验证 XML。

详细文档

createCdata(text)

创建具有给定值的未附加 CDATASection 节点。

参数

名称类型说明
textString要设置的值

返回

Cdata - 新创建的 CDATASection 节点


createComment(text)

创建具有给定值的未附加 Comment 节点。

参数

名称类型说明
textString要设置的值

返回

Comment - 新创建的 Comment 节点


createDocType(elementName)

为根 Element 节点创建一个名为给定名称的未附加 DocumentType 节点。

参数

名称类型说明
elementNameString要在 DocType 声明中指定的根 Element 节点的名称

返回

DocType - 新创建的 DocumentType 节点


createDocType(elementName, systemId)

为根 Element 节点创建一个未附加的 DocumentType 节点,并为外部子集数据提供给定的名称和系统 ID。

参数

名称类型说明
elementNameString要在 DocType 声明中指定的根 Element 节点的名称
systemIdString要设置的外部子集数据的系统 ID

返回

DocType - 新创建的 DocumentType 节点


createDocType(elementName, publicId, systemId)

为根 Element 节点创建一个未附加的 DocumentType 节点,并为外部子集数据提供给定的名称、公共 ID 和系统 ID。

参数

名称类型说明
elementNameString要在 DocType 声明中指定的根 Element 节点的名称
publicIdString要设置的外部子集数据的公开 ID
systemIdString要设置的外部子集数据的系统 ID

返回

DocType - 新创建的 DocumentType 节点


createDocument()

创建一个空的 XML 文档。

返回

Document - 新创建的文档


createDocument(rootElement)

使用给定的根 Element 节点创建 XML 文档。

参数

名称类型说明
rootElementElement要设置的根 Element 节点

返回

Document - 新创建的文档


createElement(name)

创建具有给定本地名称且没有命名空间的未附加 Element 节点。

参数

名称类型说明
nameString要设置的本地名称

返回

Element - 新创建的 Element 节点


createElement(name, namespace)

使用指定的本地名称和命名空间创建未附加的 Element 节点。

参数

名称类型说明
nameString要设置的本地名称
namespaceNamespace要设置的命名空间

返回

Element - 新创建的 Element 节点


createText(text)

创建具有给定值的未附加 Text 节点。

参数

名称类型说明
textString要设置的值

返回

Text - 新创建的 Text 节点


getCompactFormat()

创建 Format 对象以输出紧凑的 XML 文档。该格式设置默认为 UTF-8 编码,不缩进,也不添加额外的换行符,但会包含 XML 声明及其编码。

// 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);

返回

Format - 新创建的格式化程序


getNamespace(uri)

使用给定 URI 创建 Namespace

参数

名称类型说明
uriString命名空间的 URI

返回

Namespace - 新创建的命名空间


getNamespace(prefix, uri)

使用给定的前缀和 URI 创建 Namespace

参数

名称类型说明
prefixString命名空间的前缀
uriString命名空间的 URI

返回

Namespace - 新创建的命名空间


getNoNamespace()

创建一个 Namespace,表示不存在实际命名空间。

返回

Namespace - 新创建的命名空间


getPrettyFormat()

创建 Format 对象以输出可读性良好的 XML 文档。该格式设置默认为 UTF-8 编码、两空格缩进、每个节点后面都有 \r\n 行分隔符,并包含 XML 声明及其编码。

// 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);

返回

Format - 新创建的格式化程序


getRawFormat()

创建 Format 对象以输出原始 XML 文档。格式设置器默认采用 UTF-8 编码,不进行缩进,也不会添加除 XML 文档本身提供的行分隔符以外的行分隔符,并且包含 XML 声明及其编码。

// 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);

返回

Format - 新创建的格式化程序


getXmlNamespace()

创建使用标准 xml 前缀的 Namespace

返回

Namespace - 新创建的命名空间


parse(xml)

使用给定 XML 创建 Document,但不会验证 XML。

const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>'; const doc = XmlService.parse(xml);

参数

名称类型说明
xmlString要解析的 XML

返回

Document - 新创建的文档