Click here to see your recently viewed pages and most viewed pages.
Hide
Apps Script

XML Service Service

XML Service

This service allows scripts to parse, navigate, and programmatically create XML documents.

// Log the title and labels for the first page of blog posts on the Google Apps Developer blog.
function parseXml() {
  var url = 'http://googleappsdeveloper.blogspot.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');

  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var categoryElements = entries[i].getChildren('category', atom);
    var labels = [];
    for (var 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() {
  var root = XmlService.createElement('threads');
  var threads = GmailApp.getInboxThreads();
  for (var i = 0; i < threads.length; i++) {
    var child = XmlService.createElement('thread')
        .setAttribute('messageCount', threads[i].getMessageCount())
        .setAttribute('isUnread', threads[i].isUnread())
        .setText(threads[i].getFirstMessageSubject());
    root.addContent(child);
  }
  var document = XmlService.createDocument(root);
  var xml = XmlService.getPrettyFormat().format(document);
  Logger.log(xml);
}

Classes

NameBrief description
AttributeA representation of an XML attribute.
CdataA representation of an XML CDATASection node.
CommentA representation of an XML Comment node.
ContentA representation of a generic XML node.
ContentTypeAn enumeration representing the types of XML content nodes.
DocTypeA representation of an XML DocumentType node.
DocumentA representation of an XML document.
ElementA representation of an XML Element node.
EntityRefA representation of an XML EntityReference node.
FormatA formatter for outputting an XML document, with three pre-defined formats that can be further customized.
NamespaceA representation of an XML namespace.
ProcessingInstructionA representation of an XML ProcessingInstruction node.
TextA representation of an XML Text node.
XmlServiceThis service allows scripts to parse, navigate, and programmatically create XML documents.

Class Attribute

Methods

MethodReturn typeBrief description
getName()StringGets the local name of the attribute.
getNamespace()NamespaceGets the namespace for the attribute.
getValue()StringGets the value of the attribute.
setName(name)AttributeSets the local name of the attribute.
setNamespace(namespace)AttributeSets the namespace for the attribute.
setValue(value)AttributeSets the value of the attribute.

Class Cdata

Methods

MethodReturn typeBrief description
append(text)TextAppends the given text to any content that already exists in the node.
detach()ContentDetaches the node from its parent Element node.
getParentElement()ElementGets the node's parent Element node.
getText()StringGets the text value of the Text node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
setText(text)TextSets the text value of the Text node.

Class Comment

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parent Element node.
getParentElement()ElementGets the node's parent Element node.
getText()StringGets the text value of the Comment node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
setText(text)CommentSets the text value of the Comment node.

Class Content

Methods

MethodReturn typeBrief description
asCdata()CdataCasts the node as a CDATASection node for the purposes of autocomplete.
asComment()CommentCasts the node as a Comment node for the purposes of autocomplete.
asDocType()DocTypeCasts the node as a DocumentType node for the purposes of autocomplete.
asElement()ElementCasts the node as an Element node for the purposes of autocomplete.
asEntityRef()EntityRefCasts the node as a EntityReference node for the purposes of autocomplete.
asProcessingInstruction()ProcessingInstructionCasts the node as a ProcessingInstruction node for the purposes of autocomplete.
asText()TextCasts the node as a Text node for the purposes of autocomplete.
detach()ContentDetaches the node from its parent Element node.
getParentElement()ElementGets the node's parent Element node.
getType()ContentTypeGets the node's content type.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.

Class ContentType

Properties

PropertyTypeDescription
CDATAEnumAn XML CDATASection node.
COMMENTEnumAn XML Comment node.
DOCTYPEEnumAn XML DocumentType node.
ELEMENTEnumAn XML Element node.
ENTITYREFEnumAn XML EntityReference node.
PROCESSINGINSTRUCTIONEnumAn XML ProcessingInstruction node.
TEXTEnumAn XML Text node.

Class DocType

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parent Element node.
getElementName()StringGets the name of the root Element node specified in the DocType declaration.
getInternalSubset()StringGets the internal subset data for the DocumentType node.
getParentElement()ElementGets the node's parent Element node.
getPublicId()StringGets the public ID of the external subset data for the DocumentType node.
getSystemId()StringGets the system ID of the external subset data for the DocumentType node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
setElementName(name)DocTypeSets the name of the root Element node to specify in the DocType declaration.
setInternalSubset(data)DocTypeSets the internal subset data for the DocumentType node.
setPublicId(id)DocTypeSets the public ID of the external subset data for the DocumentType node.
setSystemId(id)DocTypeSets the system ID of the external subset data for the DocumentType node.

Class Document

Methods

MethodReturn typeBrief description
addContent(content)DocumentAppends the given node to the end of the document.
addContent(index, content)DocumentInserts the given node at the given index among all nodes that are immediate children of the document.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the document.
detachRootElement()ElementDetaches and returns the document's root Element node.
getAllContent()Content[]Gets all nodes that are immediate children of the document.
getContent(index)ContentGets the node at the given index among all nodes that are immediate children of the document.
getContentSize()IntegerGets the number of nodes that are immediate children of the document.
getDescendants()Content[]Gets all nodes that are direct or indirect children of the document, in the order they appear in the document.
getDocType()DocTypeGets the document's DocType declaration.
getRootElement()ElementGets the document's root Element node.
hasRootElement()BooleanDetermines whether the document has a root Element node.
removeContent()Content[]Removes all nodes that are immediate children of the document.
removeContent(content)BooleanRemoves the given node, if the node is an immediate child of the document.
removeContent(index)ContentRemoves the node at the given index among all nodes that are immediate children of the document.
setDocType(docType)DocumentSets the document's DocType declaration.
setRootElement(element)DocumentSets the document's root Element node.

Class Element

Methods

MethodReturn typeBrief description
addContent(content)ElementAppends the given node as the last child of the Element node.
addContent(index, content)ElementInserts the given node at the given index among all nodes that are immediate children of the Element node.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the {@code Element} node.
detach()ContentDetaches the node from its parent Element node.
getAllContent()Content[]Gets all nodes that are immediate children of the {@code Element} node.
getAttribute(name)AttributeGets the attribute for this Element node with the given name and no namespace.
getAttribute(name, namespace)AttributeGets the attribute for this Element node with the given name and namespace.
getAttributes()Attribute[]Gets all attributes for this Element node, in the order they appear in the document.
getChild(name)ElementGets the first Element node with the given name and no namespace that is an immediate child of this Element node.
getChild(name, namespace)ElementGets the first Element node with the given name and namespace that is an immediate child of this Element node.
getChildText(name)StringGets the text value of the node with the given name and no namespace, if the node is an immediate child of the Element node.
getChildText(name, namespace)StringGets the text value of the node with the given name and namespace, if the node is an immediate child of the Element node.
getChildren()Element[]Gets all Element nodes that are immediate children of this Element node, in the order they appear in the document.
getChildren(name)Element[]Gets all Element nodes with the given name and no namespace that are immediate children of this Element node, in the order they appear in the document.
getChildren(name, namespace)Element[]Gets all Element nodes with the given name and namespace that are immediate children of this Element node, in the order they appear in the document.
getContent(index)ContentGets the node at the given index among all nodes that are immediate children of the {@code Element} node.
getContentSize()IntegerGets the number of nodes that are immediate children of the {@code Element} node.
getDescendants()Content[]Gets all nodes that are direct or indirect children of the {@code Element} node, in the order they appear in the document.
getDocument()DocumentGets the XML document that contains the {@code Element} node.
getName()StringGets the local name of the Element node.
getNamespace()NamespaceGets the namespace for the Element node.
getNamespace(prefix)NamespaceGets the namespace with the given prefix for the Element node.
getParentElement()ElementGets the node's parent Element node.
getQualifiedName()StringGets the local name and namespace prefix of the Element node, in the form [namespacePrefix]:[localName].
getText()StringGets the text value of the Element node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
isAncestorOf(other)BooleanDetermines whether this Element node is a direct or indirect parent of a given Element node.
isRootElement()BooleanDetermines whether the Element node is the document's root node.
removeAttribute(attribute)BooleanRemoves the given attribute for this Element node, if such an attribute exists.
removeAttribute(attributeName)BooleanRemoves the attribute for this Element node with the given name and no namespace, if such an attribute exists.
removeAttribute(attributeName, namespace)BooleanRemoves the attribute for this Element node with the given name and namespace, if such an attribute exists.
removeContent()Content[]Removes all nodes that are immediate children of the {@code Element} node.
removeContent(content)BooleanRemoves the given node, if the node is an immediate child of the {@code Element} node.
removeContent(index)ContentRemoves the node at the given index among all nodes that are immediate children of the {@code Element} node.
setAttribute(attribute)ElementSets the given attribute for this Element node.
setAttribute(name, value)ElementSets the attribute for this Element node with the given name, value, and no namespace.
setAttribute(name, value, namespace)ElementSets the attribute for this Element node with the given name, value, and namespace.
setName(name)ElementSets the local name of the Element node.
setNamespace(namespace)ElementSets the namespace for the Element node.
setText(text)ElementSets the text value of the Element node.

Class EntityRef

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parent Element node.
getName()StringGets the name of the EntityReference node.
getParentElement()ElementGets the node's parent Element node.
getPublicId()StringGets the public ID of the EntityReference node.
getSystemId()StringGets the system ID of the EntityReference node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
setName(name)EntityRefSets the name of the EntityReference node.
setPublicId(id)EntityRefSets the public ID of the EntityReference node.
setSystemId(id)EntityRefSets the system ID of the EntityReference node.

Class Format

Methods

MethodReturn typeBrief description
format(document)StringOutputs the given Document as a formatted string.
format(element)StringOutputs the given Element node as a formatted string.
setEncoding(encoding)FormatSets the character encoding that the formatter should use.
setIndent(indent)FormatSets the string used to indent child nodes relative to their parents.
setLineSeparator(separator)FormatSets the string to insert whenever the formatter would normally insert a line break.
setOmitDeclaration(omitDeclaration)FormatSets whether the formatter should omit the XML declaration, such as <?xml version="1.0" encoding="UTF-8"?>.
setOmitEncoding(omitEncoding)FormatSets whether the formatter should omit the encoding in the XML declaration, such as the encoding field in <?xml version="1.0" encoding="UTF-8"?>.

Class Namespace

Methods

MethodReturn typeBrief description
getPrefix()StringGets the prefix for the namespace.
getURI()StringGets the URI for the namespace.

Class ProcessingInstruction

Methods

MethodReturn typeBrief description
detach()ContentDetaches the node from its parent Element node.
getData()StringGets the raw data for every instruction in the ProcessingInstruction node.
getParentElement()ElementGets the node's parent Element node.
getTarget()StringGets the target for the ProcessingInstruction node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.

Class Text

Methods

MethodReturn typeBrief description
append(text)TextAppends the given text to any content that already exists in the node.
detach()ContentDetaches the node from its parent Element node.
getParentElement()ElementGets the node's parent Element node.
getText()StringGets the text value of the Text node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the order they appear in the document.
setText(text)TextSets the text value of the Text node.

Class XmlService

Properties

PropertyTypeDescription
ContentTypesContentTypeAn enumeration representing the types of XML content nodes.

Methods

MethodReturn typeBrief description
createCdata(text)CdataCreates an unattached CDATASection node with the given value.
createComment(text)CommentCreates an unattached Comment node with the given value.
createDocType(elementName)DocTypeCreates an unattached DocumentType node for the root Element node with the given name.
createDocType(elementName, systemId)DocTypeCreates an unattached DocumentType node for the root Element node with the given name, and the given system ID for the external subset data.
createDocType(elementName, publicId, systemId)DocTypeCreates an unattached DocumentType node for the root Element node with the given name, and the given public ID and system ID for the external subset data.
createDocument()DocumentCreates an empty XML document.
createDocument(rootElement)DocumentCreates an XML document with the given root Element node.
createElement(name)ElementCreates an unattached Element node with the given local name and no namespace.
createElement(name, namespace)ElementCreates an unattached Element node with the given local name and namespace.
createText(text)TextCreates an unattached Text node with the given value.
getCompactFormat()FormatCreates a Format object for outputting a compact XML document.
getNamespace(uri)NamespaceCreates a Namespace with the given URI.
getNamespace(prefix, uri)NamespaceCreates a Namespace with the given prefix and URI.
getNoNamespace()NamespaceCreates a Namespace that represents the absence of a real namespace.
getPrettyFormat()FormatCreates a Format object for outputting a human-readable XML document.
getRawFormat()FormatCreates a Format object for outputting a raw XML document.
getXmlNamespace()NamespaceCreates a Namespace with the standard xml prefix.
parse(xml)DocumentCreates an Document from the given XML, without validating the XML.