XML
Stay organized with collections
Save and categorize content based on your preferences.
Parse XML
function parseXml() {
// Load an XML representation of your campaigns.
const 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('');
const document = XmlService.parse(xml);
const root = document.getRootElement();
const entries = document.getRootElement().getChildren('campaign');
for (let i = 0; i < entries.length; i++) {
const id = entries[i].getAttribute('id').getValue();
const name = entries[i].getText();
console.log('%s) %s (%s)', (i + 1).toFixed(), name, id);
}
}
Create XML
function createXml() {
// Create and log an XML representation of your campaigns.
const root = XmlService.createElement('campaigns');
const campaignIterator = AdsApp.campaigns().get();
while (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
const child = XmlService.createElement('campaign')
.setAttribute('id', campaign.getId().toFixed(0))
.setText(campaign.getName());
root.addContent(child);
}
const document = XmlService.createDocument(root);
const xml = XmlService.getPrettyFormat().format(document);
console.log(xml);
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-20 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-20 UTC."],[[["\u003cp\u003eThe provided Google Apps Script code snippets demonstrate how to parse and create XML data for Google Ads campaigns.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eparseXml\u003c/code\u003e function reads XML data, extracts campaign ID and name, and logs them to the console.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ecreateXml\u003c/code\u003e function iterates through existing Google Ads campaigns, generates XML elements for each, and logs the formatted XML output.\u003c/p\u003e\n"]]],[],null,["# XML\n\nParse XML\n---------\n\n```transact-sql\nfunction parseXml() {\n // Load an XML representation of your campaigns.\n const xml = [\n '\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e',\n '\u003ccampaigns\u003e',\n '\u003ccampaign id=\"28632346\"\u003ePlacement Campaign 1\u003c/campaign\u003e',\n '\u003ccampaign id=\"28780216\"\u003eCampaign #14\u003c/campaign\u003e',\n '\u003ccampaign id=\"29606506\"\u003eLotsOfExclusion\u003c/campaign\u003e',\n '\u003c/campaigns\u003e'\n ].join('');\n\n const document = XmlService.parse(xml);\n const root = document.getRootElement();\n\n const entries = document.getRootElement().getChildren('campaign');\n for (let i = 0; i \u003c entries.length; i++) {\n const id = entries[i].getAttribute('id').getValue();\n const name = entries[i].getText();\n console.log('%s) %s (%s)', (i + 1).toFixed(), name, id);\n }\n}\n```\n\nCreate XML\n----------\n\n```gdscript\nfunction createXml() {\n // Create and log an XML representation of your campaigns.\n const root = XmlService.createElement('campaigns');\n const campaignIterator = AdsApp.campaigns().get();\n\n while (campaignIterator.hasNext()) {\n const campaign = campaignIterator.next();\n\n const child = XmlService.createElement('campaign')\n .setAttribute('id', campaign.getId().toFixed(0))\n .setText(campaign.getName());\n root.addContent(child);\n }\n const document = XmlService.createDocument(root);\n const xml = XmlService.getPrettyFormat().format(document);\n console.log(xml);\n}\n```"]]