我编写了以下代码,用于创建XML文档,然后将其发送到外部API。
range(1, newTabAmount + 1).subscribe(x => {
templateData.forEach((templateDataElement, i) => {
if (i !== 0) {
//clone intial item
const item: XMLDocument = xmlDoc.getElementsByTagName('a:ChangeSetEntry')[0];
const target: XMLDocument = xmlDoc.getElementsByTagName('changeSet')[0];
const clone: Node = item.cloneNode(true);
target.appendChild(clone);
}
const changeSetEntry: any = xmlDoc.getElementsByTagName('a:ChangeSetEntry')[i];
const entity: XMLDocument = changeSetEntry.getElementsByTagName('a:Entity')[0];
console.log('repeat-position: ' + (newTabStartId + x));
entity.getElementsByTagName('c:RepeatPosition')[0].textContent = String(newTabStartId + x); //set amout of tab you want to add
entity.getElementsByTagName('c:MetaAttributeKey')[0].textContent = String(templateDataElement.MetaAttributeKey);
entity.getElementsByTagName('c:ReferenceKey')[0].textContent = String(referenceKey);
console.log('current id: ' + curId);
console.log('iterator;' + idIterator);
changeSetEntry.getElementsByTagName('a:Id')[0].textContent = String(idIterator);
changeSetEntry.getElementsByTagName('a:Operation')[0].textContent = 'Insert';
++idIterator;
});
});这段代码提供了以下输出:
重复位置: 11迭代器;0重复位置: 11迭代器;1>重复位置: 11迭代;2重复-位置: 11迭代;3>重复-位置: 12迭代;4重复-位置: 12迭代;5>重复-位置: 12迭代;6重复-位置: 12迭代;7>重复-位置: 13迭代;8重复-位置: 13迭代;9>重复-位置: 13迭代;10重复-位置: 13迭代;11>重复-位置: 14;12重复-位置: 14;13>重复-位置: 14迭代;14个重复位置:14个迭代器;15>请求开始.
此外,我还得到了一个XML作为输出,与console.log() -输出相比,它的顺序完全不同。
XML-中的输出:

有趣的是,前4个元素(来自内部循环的第一个迭代)有一个有效的Id。但是接下来的4个元素都有值4,(我没有添加这个值,否则屏幕截图会太大),从迭代8到12,所有元素都有Id 8。您知道为什么XML文档中的顺序与console.log()输出不同吗?不幸的是,我对此没有任何解释。
发布于 2019-08-08 12:41:21
我找到了解决问题的办法:
range(1, newTabAmount).subscribe(x => {
templateData.forEach((templateDataElement, i) => {
const xmlRawDataElement: any = {};
xmlRawDataElement.repeatPos = newTabStartId + x;
xmlRawDataElement.Id = idIterator;
xmlRawData.push(xmlRawDataElement);
++idIterator;
});
});通过首先构建一个可评估的模型,然后将其应用到我的XML模型中,它可以工作。我假设,由于XML操作需要一些时间,angular尝试在另一个线程中执行其他任务,即使下一步的必要变量还没有正确设置。因此,如果您遇到同样的问题,错误的顺序,尝试构建您的XML模型的要点在一个小的JSON,并在之后,如果您已经正确地计算了一切:)
https://stackoverflow.com/questions/57389937
复制相似问题