Google Apps Script:
Working with XML

How to:

Google Apps Script provides the XmlService to work with XML data. Below we demonstrate how to parse an XML string, modify its contents, and generate a new XML string.

Parsing an XML string:

function parseXML() {
  var xmlString = '<root><child name="first">Hello</child><child name="second">World</child></root>';
  var document = XmlService.parse(xmlString);
  var root = document.getRootElement();
  var children = root.getChildren('child');
  Logger.log(children[0].getText()); // Logs: Hello
}

To modify the XML, you might want to add a new child element:

function addNewChild() {
  var xmlString = '<root><child name="first">Hello</child></root>';
  var document = XmlService.parse(xmlString);
  var root = document.getRootElement();
  
  var newChild = XmlService.createElement('child').setText('World');
  root.addContent(newChild);
  
  var xml = XmlService.getPrettyFormat().format(document);
  Logger.log(xml);
  // Logs the new XML string with the added child element
}

Generating XML string from scratch:

function createXML() {
  var root = XmlService.createElement('root');
  var child = XmlService.createElement('child').setText('Hello World');
  root.addContent(child);
  
  var xml = XmlService.getPrettyFormat().format(XmlService.createDocument(root));
  Logger.log(xml);
  // Outputs: <root><child>Hello World</child></root>
}

Deep Dive

Historically, XML (Extensible Markup Language) was the de facto standard for data interchange before JSON emerged as a lightweight alternative. XML’s verbose syntax and strict parsing model provided a robust, albeit bulky, data format. In Google Apps Script, the XmlService API encapsulates the creation, parsing, and manipulation of XML data, acknowledging its continued importance in various legacy and enterprise systems, SOAP web services, and configuration files for applications.

Despite JSON’s prevalence in modern web development for its simplicity and ease of use with JavaScript, XML remains relevant in areas where document validation and structured hierarchies are crucial. However, for new projects, especially those leaning towards web APIs, JSON is often the more practical choice due to its lightweight nature and seamless integration with JavaScript.

Understanding XML and its handling in Google Apps Script is paramount for developers working in environments where integration with older systems or specific enterprise APIs is necessary. However, when starting new projects or when flexibility is key, evaluating the need for XML over alternatives like JSON is advisable.