【发布时间】:2020-06-03 18:33:42
【问题描述】:
我正在讨论获取数组并将其添加到 XML 对象的最佳方法。
我从一个具有空节点的 XML 对象开始。示例 XML:
<Request>
... other nodes ...
<Test></Test>
<Items>
<Item>
<Class></Class>
<Weight></Weight>
</Item>
</Items>
... other nodes ...
</Request>
我已经解析了上面的XML,可以在对象上设置数据就好了:
<cfset ParsedXML.Request.Test.XMLText = "Test">
结果如下:
<Request>
... other nodes ...
<Test>Test</Test>
<Items>
<Item>
<Class></Class>
<Weight></Weight>
</Item>
</Items>
... other nodes ...
</Request>
到目前为止一切顺利。但是,当我想获取一个 Coldfusion 数组并将其添加到 XMLChildren 时,我遇到了问题。所以说我拿了一组物品:
<cfset ItemsArray = ArrayNew(1)>
<cfset ItemsArray[1] = {
"Class": 55,
"Weight": 100
}>
<cfset ItemsArray[2] = {
"Class": 55,
"Weight": 200
}>
然后我想遍历该数组以在 ResponseNodes.Request.Items.XMLChildren 中创建新节点:
<cfset ItemRow = 1>
<cfloop array="#ItemsArray#" index="i">
<cfset ParsedXML.Request.Items.Item[ItemRow].Class.XMLText = i.Class>
<cfset ParsedXML.Request.Items.Item[ItemRow].Weight.XMLText = i.Weight>
<cfset ItemRow = ItemRow + 1>
</cfloop>
我收到此错误:
子元素的索引超出范围。 该节点下只有 1 个子节点。 索引 2 超出允许范围 [1-1]。
我也尝试过 XmlElemNew() 但一直遇到The right hand side of the assignment is not of type XML Node.
【问题讨论】:
标签: xml coldfusion cfml coldfusion-11