บริการนี้ช่วยให้สคริปต์แยกวิเคราะห์ นำทาง และสร้างเอกสาร 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 | สร้างโหนด CDATASection ที่ไม่ได้แนบมาพร้อมค่าที่ระบุ |
create | Comment | สร้างโหนด Comment ที่ไม่ได้แนบมาพร้อมค่าที่ระบุ |
create | Doc | สร้างโหนด Document ที่ไม่ได้แนบสำหรับโหนดรูท Element
ที่มีชื่อที่ระบุ |
create | Doc | สร้างโหนด Document ที่ไม่ได้แนบสำหรับโหนด Element รูท
โดยใช้ชื่อที่ระบุและรหัสระบบที่ระบุสำหรับข้อมูลเซ็ตย่อยภายนอก |
create | Doc | สร้างโหนด Document ที่ไม่ได้แนบสำหรับโหนด Element รูท
โดยใช้ชื่อที่ระบุ รวมถึงรหัสสาธารณะและรหัสระบบที่ระบุสำหรับข้อมูลเซ็ตย่อยภายนอก |
create | Document | สร้างเอกสาร XML ว่างเปล่า |
create | Document | สร้างเอกสาร XML ที่มีโหนดราก Element ที่ระบุ |
create | Element | สร้างโหนด Element ที่ไม่ได้แนบโดยใช้ชื่อเฉพาะที่ระบุและไม่มีเนมสเปซ |
create | Element | สร้างโหนด Element ที่ไม่ได้แนบด้วยชื่อและเนมสเปซในเครื่องที่กำหนด |
create | Text | สร้างโหนด Text ที่ไม่ได้แนบมาพร้อมค่าที่ระบุ |
get | Format | สร้างออบเจ็กต์ Format สำหรับเอาต์พุตเอกสาร XML แบบย่อ |
get | Namespace | สร้าง Namespace ด้วย URI ที่ระบุ |
get | Namespace | สร้าง Namespace ด้วยคำนำหน้าและ URI ที่ระบุ |
get | Namespace | สร้าง Namespace ที่แสดงถึงการไม่มีเนมสเปซจริง |
get | Format | สร้างออบเจ็กต์ Format สำหรับเอาต์พุตเอกสาร XML ที่มนุษย์อ่านได้ |
get | Format | สร้างออบเจ็กต์ Format สำหรับเอาต์พุตเอกสาร XML ดิบ |
get | Namespace | สร้าง Namespace โดยมีคำนำหน้า xml มาตรฐาน |
parse(xml) | Document | สร้าง Document จาก XML ที่ระบุโดยไม่ต้องตรวจสอบ XML |
เอกสารโดยละเอียด
createCdata(text)
สร้างโหนด CDATASection ที่ไม่ได้แนบมาพร้อมค่าที่ระบุ
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
text | String | ค่าที่จะตั้ง |
รีเทิร์น
Cdata - โหนด CDATASection ที่สร้างขึ้นใหม่
createComment(text)
createDocType(elementName)
สร้างโหนด DocumentType ที่ไม่ได้แนบสำหรับโหนดรูท Element
ที่มีชื่อที่ระบุ
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
element | String | ชื่อของโหนดรูท Element ที่จะระบุในการประกาศ Doc
|
รีเทิร์น
DocType - โหนด DocumentType ที่สร้างขึ้นใหม่
createDocType(elementName, systemId)
สร้างโหนด DocumentType ที่ไม่ได้แนบสำหรับโหนด Element รูท
โดยใช้ชื่อที่ระบุและรหัสระบบที่ระบุสำหรับข้อมูลเซ็ตย่อยภายนอก
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
element | String | ชื่อของโหนดรูท Element ที่จะระบุในการประกาศ Doc
|
system | String | รหัสระบบของข้อมูลเซ็ตย่อยภายนอกที่จะตั้งค่า |
รีเทิร์น
DocType - โหนด DocumentType ที่สร้างขึ้นใหม่
createDocType(elementName, publicId, systemId)
สร้างโหนด DocumentType ที่ไม่ได้แนบสำหรับโหนด Element รูท
โดยใช้ชื่อที่ระบุ รวมถึงรหัสสาธารณะและรหัสระบบที่ระบุสำหรับข้อมูลเซ็ตย่อยภายนอก
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
element | String | ชื่อของโหนดรูท Element ที่จะระบุในการประกาศ Doc
|
public | String | รหัสสาธารณะของข้อมูลเซ็ตย่อยภายนอกที่จะตั้งค่า |
system | String | รหัสระบบของข้อมูลเซ็ตย่อยภายนอกที่จะตั้งค่า |
รีเทิร์น
DocType - โหนด DocumentType ที่สร้างขึ้นใหม่
createDocument()
createDocument(rootElement)
createElement(name)
createElement(name, namespace)
createText(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)
getNamespace(prefix, uri)
getNoNamespace()
getPrettyFormat()
สร้างออบเจ็กต์ Format สำหรับเอาต์พุตเอกสาร XML ที่มนุษย์อ่านได้ ตัวจัดรูปแบบ
จะใช้การเข้ารหัส UTF-8 โดยค่าเริ่มต้น การเยื้อง 2 ช่องว่าง ตัวคั่นบรรทัด \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 - ตัวจัดรูปแบบที่สร้างขึ้นใหม่