XML

Parse XML

function parseXml() {
  // Load an XML representation of your campaigns.
  var xml = [
      '<?xml version="1.0" encoding="UTF-8"?>',
      '<campaigns>',
        '<campaign id="28632346">Placement Campaign 1</campaign>',
        '<campaign id="28780216">Campaign #14</campaign>',
        '<campaign id="29606506">LotsOfExclusion</campaign>',
      '</campaigns>'
  ].join('');

  var document = XmlService.parse(xml);
  var root = document.getRootElement();

  var entries = document.getRootElement().getChildren('campaign');
  for (var i = 0; i < entries.length; i++) {
    var id = entries[i].getAttribute('id').getValue();
    var name = entries[i].getText();
    Logger.log('%s) %s (%s)', (i + 1).toFixed(), name, id);
  }
}

Create XML

function createXml() {
  // Create and log an XML representation of your campaigns.
  var root = XmlService.createElement('campaigns');
  var campaignIterator = AdWordsApp.campaigns().get();

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();

    var child = XmlService.createElement('campaign')
        .setAttribute('id', campaign.getId().toFixed(0))
        .setText(campaign.getName());
    root.addContent(child);
  }
  var document = XmlService.createDocument(root);
  var xml = XmlService.getPrettyFormat().format(document);
  Logger.log(xml);
}

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.