• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

StructKeyExists and 2 arrays

Explorer ,
Apr 12, 2007 Apr 12, 2007

Copy link to clipboard

Copied

I am working with an XML object with many optional elements. I am trying to use StructKeyExists to see if optional elements are indeed populated, but I can't figure out how to code the statement. Right now, I have given up and used the <cftry> option as shown below and it is working fine, but I understand that the StructKeyExists is more efficient.

This is working...
<cftry>
<cfset varCompanyName = xmlQuoteIn.Quotes.Quote.Policies.Policy.Company.Name.XmlText>
<cfcatch type="any"><cfset varCompanyName=""></cfcatch>
</cftry>

My attempt at using StructKeyExists looks like this (but it doesn't work)...
<cfif StructKeyExists(xmlQuoteIn.Quotes.Quote.Policies.Policy,"Company.Name")>

Is there a way to make the above "StructKeyExists" statement work?

Thank you,
David Josephs


TOPICS
Advanced techniques

Views

654

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 12, 2007 Apr 12, 2007

Copy link to clipboard

Copied

Hi.

Dump the XML structure to see how you actually set it up your parent child relation...It could be this :
Here is an example:
<cfif StructKeyExists(myXml.RootNode.ChildNode.SomeNodeArray[1]["ChildNodeInArray"], "NameOfNodeIAmLookingFor")>
Hurray!
<cfelse>
Booo
</cfif>

Good luck.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 13, 2007 Apr 13, 2007

Copy link to clipboard

Copied

I don't think you can use structKeyExists to test for multiple or nested keys. Try a separate check for each.

<cfif
StructKeyExists(xmlQuoteIn.Quotes.Quote.Policies.Policy,"Company")
AND
StructKeyExists(xmlQuoteIn.Quotes.Quote.Policies.Policy.Company,"Name")
>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2007 Apr 14, 2007

Copy link to clipboard

Copied

I'd perhaps use xmlSearch() in your case.

But the other poster was correct in that if you do structKeyExists(st,
"foo.bar") it'll test for a KEY called "foo.bar", not for a sub-struct
"foo" with a key "bar".

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2007 Apr 14, 2007

Copy link to clipboard

Copied

LATEST
> My attempt at using StructKeyExists looks like this (but it doesn't work)...
<cfif StructKeyExists(xmlQuoteIn.Quotes.Quote.Policies.Policy,"Company.Name")>


This should tell you why it doesn't work:
<cfoutput>#StructKeyList(xmlQuoteIn.Quotes.Quote.Policies.Policy)#</cfoutput>


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation