gpt4 book ai didi

xml - 检查 XML 是否存在特定键

转载 作者:数据小太阳 更新时间:2023-10-29 02:05:58 25 4
gpt4 key购买 nike

在下面的 XML 中,我想看看是否有名为“errors”的元素

<cfxml variable="sXML">
<?xml version="1.0" encoding="UTF-8"?>
<createTransactionResponse>
<messages>
<message>
<text>
<XmlText>The transaction was unsuccessful.</XmlText>
</text>
</message>
</messages>
<transactionResponse>
<errors>
<error>
<errorText>
<XmlText>The credit card number is invalid.</XmlText>
</errorText>
</error>
</errors>
</transactionResponse>
</createTransactionResponse>
</cfxml>

为了查看节点“错误”是否存在,我使用了:

<cfif structKeyExists(sXML, "errors")>

但它返回 false(如果事务成功,则 XML 没有节点“错误”)。我做错了什么或者有更好的方法吗?

最佳答案

如果使用 <cfdump var="#sXML#"> 转储 xml 结构,它表明“errors”是一个子节点,向下几层:

Dump of sXML nodes

您可以通过父结构引用它。假设 xml 始终包含父节点“createTransactionResponse”和“transactionResponse”,使用:

<cfif structKeyExists(sXML.createTransactionResponse.transactionResponse, "errors")>
Found
<cfelse>
Not Found
</cfif>

关于xml - 检查 XML 是否存在特定键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52248462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com