如何在 Coldfusion 中将数组添加到 XML 对象?
How do I add an array to an XML object in Coldfusion?
我不知道获取数组并将其添加到 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>
我收到这个错误:
The index of a child element is out of range.
There are only 1 children under this node.
Index 2 is out of the allowed range [1-1].
我也尝试过 XmlElemNew() 但将 运行 保留为 The right hand side of the assignment is not of type XML Node.
这就是您要实现的目标吗?您需要将您尝试添加的任何节点(Item
、Class
、Weight
等)作为 XmlChildren
添加到您的 xml。
<cfset ItemsArray = [
{"Class": 55, "Weight": 100},
{"Class": 55, "Weight": 200},
{"Class": 55, "Weight": 300}
]>
<cfxml variable="ParsedXML">
<cfoutput>
<Request>
<Test></Test>
<Items>
</Items>
</Request>
</cfoutput>
</cfxml>
<cfset ParsedXML.Request.Test.XMLText = "Test">
<cfset ItemRow = 1>
<cfloop array="#ItemsArray#" index="i">
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow] = XmlElemNew(ParsedXML,"Item")>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[1] = XmlElemNew(ParsedXML,"Class")>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[2] = XmlElemNew(ParsedXML,"Weight")>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[1].XMLText = i.Class>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[2].XMLText = i.Weight>
<cfset ItemRow += 1>
</cfloop>
<cfdump var="#ParsedXML#">
我不知道获取数组并将其添加到 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>
我收到这个错误:
The index of a child element is out of range. There are only 1 children under this node. Index 2 is out of the allowed range [1-1].
我也尝试过 XmlElemNew() 但将 运行 保留为 The right hand side of the assignment is not of type XML Node.
这就是您要实现的目标吗?您需要将您尝试添加的任何节点(Item
、Class
、Weight
等)作为 XmlChildren
添加到您的 xml。
<cfset ItemsArray = [
{"Class": 55, "Weight": 100},
{"Class": 55, "Weight": 200},
{"Class": 55, "Weight": 300}
]>
<cfxml variable="ParsedXML">
<cfoutput>
<Request>
<Test></Test>
<Items>
</Items>
</Request>
</cfoutput>
</cfxml>
<cfset ParsedXML.Request.Test.XMLText = "Test">
<cfset ItemRow = 1>
<cfloop array="#ItemsArray#" index="i">
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow] = XmlElemNew(ParsedXML,"Item")>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[1] = XmlElemNew(ParsedXML,"Class")>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[2] = XmlElemNew(ParsedXML,"Weight")>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[1].XMLText = i.Class>
<cfset ParsedXML.Request.Items.XmlChildren[ItemRow].XmlChildren[2].XMLText = i.Weight>
<cfset ItemRow += 1>
</cfloop>
<cfdump var="#ParsedXML#">