ColdFusion:我是否需要对深层结构的每个元素使用structKeyExists? [英] ColdFusion: do i need to use structKeyExists for every element of a deep struct?

查看:79
本文介绍了ColdFusion:我是否需要对深层结构的每个元素使用structKeyExists?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我刚刚解析了别人的XML文档,该文档是对API请求的响应.我想知道嵌套在内部的值是否存在.如果我的API请求有效,则每次都在同一位置.如果我的API请求失败,则XML的根将非常不同.

如果我对失败的api请求尝试<cfif structKeyExists(myStruct.level1.level2.level3, 'myTarget')>,则会收到致命错误:Element LEVEL1.LEVEL2 is undefined in MYSTRUCT.

当然,我可以尝试依靠XML的根级来告诉我成功或失败,而不是寻找失败的结果,但是...除非解决方案禁止,我应该怎么做?

我是否需要检查结构的每个级别的存在?如:

<cfif structKeyExists(myStruct, 'level1') 
  and structKeyExists(myStruct.level1, 'level2') 
    and structKeyExists(myStruct.level1.level2, 'level3') 
      and structKeyExists(myStruct.level1.level2.level3, 'myTarget')>
<!--- ... --->
</cfif>

这不是一个现实世界的问题,这只是我已经面对太多次了.请不要告诉我涉及更改API的解决方案或第三段中的解决方案.

谢谢!

我应该提到为什么我不能使用isDefined()-一些键没有语法上有效的名称,因此isDefined()会引发错误,例如myStruct.level1 [42] .level3

解决方案

XMLSearch

我将使用解析的XML文档(即xmlDoc)和 XMLSearch :

<cfset xmlDoc = xmlParse(responseData)>
<cfset nodes = XmlSearch(xmlDoc, '/level1/level2/level3/myTarget')>
<cfif arrayLen(nodes)>
    <!--- do something, you have the "nodes" array to work with too --->
</cfif>

XMLSearch()

xpath假定结构键是节点.例如,如果"myTarget"是节点的属性,则需要进行相应的修改.

StructFindKey

另一种方法是 StructFindKey .

<cfset result = structFindKey(myStruct, "myTarget")>
<cfif arrayLen(result) AND result.path EQ "level1.level2.level3">
    <!--- do something --->
</cfif>

结论

尚未测试,但我相信它会比使用IsDefined()或try-catch块更快.与XMLValidate()相比,它具有不需要DTD的优点.而且,即使使用DTD,所需的节点也可能被定义为可选节点,因此它仍然可以验证.

Let's say i've just parsed someone else's XML document which is a response to an API request. I want to know if a value nested deep inside exists. If my API request worked, it will be in the same place every time. If my API request fails, the root of the XML is very different.

If I try <cfif structKeyExists(myStruct.level1.level2.level3, 'myTarget')> on a failed api request, I get the fatal error: Element LEVEL1.LEVEL2 is undefined in MYSTRUCT.

Of course, I could try to depend on the root level of the XML telling me of success or failure, and not looking for the result if it failed, but... barring that solution, what should i do?

Do i need to check for the existence of each level of the struct? As in:

<cfif structKeyExists(myStruct, 'level1') 
  and structKeyExists(myStruct.level1, 'level2') 
    and structKeyExists(myStruct.level1.level2, 'level3') 
      and structKeyExists(myStruct.level1.level2.level3, 'myTarget')>
<!--- ... --->
</cfif>

This is not a real-world problem, this is just something i've faced too many times. Please don't tell me solutions that involve changing the API or solutions like those in the third paragraph.

Thanks!

edit: i should have mentioned why i can't use isDefined() - some of the keys do not have syntactically valid names, so isDefined() throws an error, eg myStruct.level1[42].level3

解决方案

XMLSearch

I would use the parsed XML document (i.e. xmlDoc) and XMLSearch:

<cfset xmlDoc = xmlParse(responseData)>
<cfset nodes = XmlSearch(xmlDoc, '/level1/level2/level3/myTarget')>
<cfif arrayLen(nodes)>
    <!--- do something, you have the "nodes" array to work with too --->
</cfif>

xpath for XMLSearch() assumes the structure keys are nodes. You would need to modify accordingly if, for instance, 'myTarget' is an attribute of a node.

StructFindKey

Another way of doing this would be StructFindKey.

<cfset result = structFindKey(myStruct, "myTarget")>
<cfif arrayLen(result) AND result.path EQ "level1.level2.level3">
    <!--- do something --->
</cfif>

Conclusion

Haven't tested, but I believe either will be faster than using IsDefined() or a try-catch block. Has the advantage over XMLValidate() of not needing a DTD. And, even with a DTD, the node you want may be defined as optional, so it could still validate.

这篇关于ColdFusion:我是否需要对深层结构的每个元素使用structKeyExists?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆