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 - 新建立的文件。