借助此服务,脚本可以解析、浏览和以程序化方式创建 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); }
属性
属性 | 类型 | 说明 |
---|---|---|
Content | Content | 表示 XML 内容节点类型的枚举。 |
方法
详细文档
create Cdata(text)
create Doc Type(elementName)
为根 Element
节点创建一个名为给定名称的未附加 Document
节点。
参数
名称 | 类型 | 说明 |
---|---|---|
element | String | 要在 Doc 声明中指定的根 Element 节点的名称 |
返回
Doc
- 新创建的 Document
节点
create Doc Type(elementName, systemId)
为根 Element
节点创建一个未附加的 Document
节点,并为外部子集数据提供给定的名称和系统 ID。
参数
名称 | 类型 | 说明 |
---|---|---|
element | String | 要在 Doc 声明中指定的根 Element 节点的名称 |
system | String | 要设置的外部子集数据的系统 ID |
返回
Doc
- 新创建的 Document
节点
create Doc Type(elementName, publicId, systemId)
为根 Element
节点创建一个未附加的 Document
节点,并为外部子集数据提供给定的名称、公共 ID 和系统 ID。
参数
名称 | 类型 | 说明 |
---|---|---|
element | String | 要在 Doc 声明中指定的根 Element 节点的名称 |
public | String | 要设置的外部子集数据的公开 ID |
system | String | 要设置的外部子集数据的系统 ID |
返回
Doc
- 新创建的 Document
节点
create Document()
create Document(rootElement)
create Element(name)
create Element(name, namespace)
get Compact Format()
创建 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
- 新创建的格式化程序
get Namespace(prefix, uri)
get Pretty Format()
创建 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
- 新创建的格式化程序
get Raw Format()
创建 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
- 新创建的格式化程序