這項服務可讓指令碼剖析、瀏覽及以程式輔助方式建立 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 內容節點的類型。 |