在下面的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>
要查看节点“errors”是否存在,我使用了:
<cfif structKeyExists(sXML, "errors")>
但它会返回
false
(如果事务成功,则xml没有节点“错误”)。我做错了什么?还是有更好的办法? 最佳答案
如果使用<cfdump var="#sXML#">
转储XML结构,则会显示“errors”是一个子节点,向下分几级:
您可以通过父结构引用它。假设XML始终包含父节点“createTransactionResponse”和“transactionResponse”,请使用:
<cfif structKeyExists(sXML.createTransactionResponse.transactionResponse, "errors")>
Found
<cfelse>
Not Found
</cfif>