XML
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
剖析 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); } }
建立 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); }
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\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,["Parse XML \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```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```"]]