이 서비스를 사용하면 스크립트가 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 Comment(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)
지정된 이름과 외부 하위 집합 데이터의 지정된 공개 ID 및 시스템 ID를 사용하여 루트 Element
노드의 연결되지 않은 Document
노드를 만듭니다.
매개변수
이름 | 유형 | 설명 |
---|---|---|
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()
좁은 XML 문서를 출력하기 위한 Format
객체를 만듭니다. 형식 지정기는 기본적으로 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(uri)
get Namespace(prefix, uri)
get Pretty Format()
사람이 읽을 수 있는 XML 문서를 출력하기 위한 Format
객체를 만듭니다. 형식 지정기는 기본적으로 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
- 새로 생성된 형식 지정 도구
get Raw Format()
원시 XML 문서를 출력하기 위한 Format
객체를 만듭니다. 형식 지정기는 기본적으로 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
- 새로 생성된 형식 지정 도구