我正在使用一些遗留代码,并且必须更新使用E4X构建的SOAP请求。我有一个数据数组,我需要循环并构建重复的XML段并将它们插入到SOAP请求中。我的伪示例......
var myData = ['apples','oranges','pears','grapes'];
var myFruit;
for( var i=0; i<myData.length; i++ ){
myFruit += <fruit>{myData[i]}</fruit>;
}
var request = <soapenv:Envelope>
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<MyFruitList>
{myFruit}
</MYFruitList>
</soapenv:Body>
</soapenv:Envelope>;
除非{myFruit}中的所有标签都显示为已转义,否则此作品除外..它看起来像这样..
<soapenv:Envelope>
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<MyFruitList>
<fruit>apples</fruit>
<fruit>oranges</fruit>
<fruit>pears</fruit>
<fruit>grapes</fruit>
</MYFruitList>
</soapenv:Body>
</soapenv:Envelope>;
..然后我的SOAP请求的另一端失败了...任何帮助?
答案 0 :(得分:1)
这个问题的最终结果相当简单,但是 biiit hacky。
当我创建我的E4X对象时,我只需放入一个占位符元素......例如......
var myData = ['apples','oranges','pears','grapes'];
var myFruit;
for( var i=0; i<myData.length; i++ ){
myFruit += <fruit>{myData[i]}</fruit>;
}
var request = <soapenv:Envelope>
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<MyFruitList>
<fruitList/>
</MyFruitList>
</soapenv:Body>
</soapenv:Envelope>;
后来,我在提交SOAP请求之前转换整个对象字符串..所以在字符串转换之后我只是做了一个字符串替换。
var requestString = request.toString();
requestString = requestString.replace("<fruitList/>",myFruit);