這項服務可讓指令碼剖析、瀏覽及透過程式建立 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 - 新建立的格式化工具。