我正在使用google adwords WSDL和soap。
我发现xml元素的顺序很重要。
这是我的请求对象:
const reportDef: IReportDefinition = {
// order is matter
dateRangeType: _.get(reportDefinition, 'dateRangeType', ReportDefinition.DateRangeType.ALL_TIME),
selector: {
fields: CampaignPerformanceReportService.selectorFields,
},
reportName: CampaignPerformanceReportService.reportName,
reportType: ReportDefinition.ReportType.CAMPAIGN_PERFORMANCE_REPORT,
};
然后,使用xml2js
构建器将此对象转换为XML
:
const builder = new xml2js.Builder();
builder.buildObject(obj);
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reportDefinition>
<dateRangeType>YESTERDAY</dateRangeType>
<selector>
<fields>CampaignId</fields>
<fields>CampaignName</fields>
<fields>CampaignStatus</fields>
<fields>StartDate</fields>
<fields>EndDate</fields>
<fields>Clicks</fields>
<fields>Conversions</fields>
<fields>Ctr</fields>
<fields>Cost</fields>
<fields>Impressions</fields>
<fields>ConversionRate</fields>
<fields>AverageCpc</fields>
</selector>
<reportName>Campaign Performance Report</reportName>
<reportType>CAMPAIGN_PERFORMANCE_REPORT</reportType>
<downloadFormat>XML</downloadFormat>
</reportDefinition>
然后发送此请求,但出现错误:
StatusCodeError: 400 - "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><reportDownloadError><ApiError><type>ReportDownloadError.INVALID_REPORT_DEFINITION_XML</type><trigger>Invalid ReportDefinition Xml: cvc-complex-type.2.4.a: Invalid content was found starting with element '{\"https://adwords.google.com/api/adwords/cm/v201809\":dateRangeType}'. One of '{\"https://adwords.google.com/api/adwords/cm/v201809\":id, \"https://adwords.google.com/api/adwords/cm/v201809\":selector}' is expected.</trigger><fieldPath></fieldPath></ApiError></reportDownloadError>"
当我将dataRangeType
字段放在最后时。喜欢:
const reportDef: IReportDefinition = {
// order is matter
selector: {
fields: CampaignPerformanceReportService.selectorFields,
},
reportName: CampaignPerformanceReportService.reportName,
reportType: ReportDefinition.ReportType.CAMPAIGN_PERFORMANCE_REPORT,
dateRangeType: _.get(reportDefinition, 'dateRangeType', ReportDefinition.DateRangeType.ALL_TIME),
};
转换后的XML
是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reportDefinition>
<selector>
<fields>CampaignId</fields>
<fields>CampaignName</fields>
<fields>CampaignStatus</fields>
<fields>StartDate</fields>
<fields>EndDate</fields>
<fields>Clicks</fields>
<fields>Conversions</fields>
<fields>Ctr</fields>
<fields>Cost</fields>
<fields>Impressions</fields>
<fields>ConversionRate</fields>
<fields>AverageCpc</fields>
</selector>
<reportName>Campaign Performance Report</reportName>
<reportType>CAMPAIGN_PERFORMANCE_REPORT</reportType>
<dateRangeType>YESTERDAY</dateRangeType>
<downloadFormat>XML</downloadFormat>
</reportDefinition>
然后我可以得到正确的答复:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<report>
<report-name name='Campaign Performance Report'/>
<date-range date='May 22, 2019'/>
</report>
那么,如何解决xml元素顺序问题?我不知道xml元素应该有什么顺序。我必须尝试一次又一次地对属性进行排序。
这些是更好的方法吗?