检查 XML 是否存在特定键

Check XML for the existence of a specific key

下面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 没有节点 "errors")。我做错了什么或者有更好的方法吗?

如果将xml结构转储为<cfdump var="#sXML#">,则表明"errors"是一个子节点,向下几层:

您可以通过父结构引用它。假设 xml 总是包含父节点 "createTransactionResponse" 和 "transactionResponse",使用:

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