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);
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):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,["# 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```"]]