0

I am trying to add a namespace to my root element only, but doing so seems to add xmlns="" to all direct child elements. This is causing the Google AdWords API to return an error.

Here is my code:

var adwordsNameSpace = XmlService.getNamespace('https://adwords.google.com/api/adwords/cm/v201809');

var root = XmlService.createElement('reportDefinition')
    .setNamespace(adwordsNameSpace)

var selector = XmlService.createElement('selector')

var fields = ['Clicks', 'Ctr', 'Cost'];

fields.forEach(function (field) {
  var xml = XmlService.createElement('fields').setText(field);
  selector.addContent(xml)
})

root.addContent(selector)

var options = [
  ['reportName', 'Custom Report'],
  ['reportType', 'ACCOUNT_PERFORMANCE_REPORT'],
  ['dateRangeType', 'LAST_7_DAYS'],
  ['downloadFormat', 'XML']
]

options.forEach(function(option) {
  root.addContent(XmlService.createElement(option[0]).setText(option[1]))
})

var document = XmlService.createDocument(root);
var xml = XmlService.getPrettyFormat().format(document);
Logger.log(xml);

And this is the final result:

<?xml version="1.0" encoding="UTF-8"?>
<reportDefinition xmlns="https://adwords.google.com/api/adwords/cm/v201809">
  <selector xmlns="">
    <fields>Clicks</fields>
    <fields>Ctr</fields>
    <fields>Cost</fields>
  </selector>
  <reportName xmlns="">Custom Report</reportName>
  <reportType xmlns="">ACCOUNT_PERFORMANCE_REPORT</reportType>
  <dateRangeType xmlns="">LAST_7_DAYS</dateRangeType>
  <downloadFormat xmlns="">XML</downloadFormat>
</reportDefinition>

How I can either prevent or easily remove those blank attributes?

1 Answer 1

2

Once you declare a namespace on some parent element, you have to continue to use that namespace on the subsequent elements. XmlService.createElement accepts a second parameter for the namespace so you don't have to call a separate function to set the namespace.

function XMLTest() {
  var adwordsNameSpace = XmlService.getNamespace("https://adwords.google.com/api/adwords/cm/v201809");
  var root = XmlService.createElement('reportDefinition', adwordsNameSpace);  
  var selector = XmlService.createElement('selector', adwordsNameSpace);
  ['Clicks', 'Ctr', 'Cost'].forEach(function (field) {
    var xml = XmlService.createElement('fields', adwordsNameSpace).setText(field);
    selector.addContent(xml)
  });
  root.addContent(selector);
  var options = {
    reportName: 'Custom Report',
    reportType: 'ACCOUNT_PERFORMANCE_REPORT',
    dateRangeType: 'LAST_7_DAYS',
    downloadFormat: 'XML'
  };
  for (var tagName in options) {
    root.addContent(XmlService.createElement(tagName, adwordsNameSpace).setText(options[tagName]))
  }
  var document = XmlService.createDocument(root);
  var xml = XmlService.getPrettyFormat().format(document);
  Logger.log(xml);
}
/* LOGGER OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<reportDefinition xmlns="https://adwords.google.com/api/adwords/cm/v201809">
  <selector>
    <fields>Clicks</fields>
    <fields>Ctr</fields>
    <fields>Cost</fields>
  </selector>
  <reportName>Custom Report</reportName>
  <reportType>ACCOUNT_PERFORMANCE_REPORT</reportType>
  <dateRangeType>LAST_7_DAYS</dateRangeType>
  <downloadFormat>XML</downloadFormat>
</reportDefinition>
*/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.