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

Test for null node in xml structure?

Guest
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

Hi,

How do I test if a node in an xml document exists?

doing this returns an error if it doesn't exist.

x = myxmldoc["my-list"]["my-item] ["user-id"]["xmltext"];

"Element user-id is undefined in a Java object of type class coldfusion.xml.XmlNodeList."

Any help would be much appreciated..

Many thanks.! :)
TOPICS
Advanced techniques

Views

1.6K

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
Advocate ,
Jun 07, 2007 Jun 07, 2007

Copy link to clipboard

Copied

Have you tried using isDefined() or StructKeyExists()?

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
Advocate ,
Jun 07, 2007 Jun 07, 2007

Copy link to clipboard

Copied

Oh, and I probably should mention - a VERY annoying habit of the adobe forum is to replace the text [ + i + ] with <i> (i.e. its the markup code that the adobe forum uses for italics). I'm guessing your above code is really:

x = myxmldoc["my-list"]["my-item][eye]["user-id"]["xmltext"];

and not some funky italic work.

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
Guest
Jun 07, 2007 Jun 07, 2007

Copy link to clipboard

Copied

Hi Michael,

Yes tried both of these it doesn't work unfortunately :(

Funny thing is, the error message on isdefined is:

user id is undefined in a Java object of type class coldfusion.xml.XmlNodeList.

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
Advocate ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

You could try getting around that using XMLSearch() + xPath. It should return an array (either empty or not) regardless of whether your element exists.

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 ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

> You could try getting around that using XMLSearch() + xPath.

This is what I'd do.

--
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
LEGEND ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

> x = myxmldoc["my-list"]["my-item] ["user-id"]["xmltext"]

There IS a missing double quote in that. I presume it's a typo.

You would certainly get a different error if that was in fact your code.

--
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
LEGEND ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

> user id is undefined in a Java object of type class coldfusion.xml.XmlNodeList.

isDefined() will never work for this sort of thing: it can only deal with
simple variable names (letters, numbers, dots, underscores, currency
symbols, etc).

structKeyExists() DOES work though:

structKeyExists(myxmldoc["my-list"]["my-item"] , "user-id")

--
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
Guest
Jun 11, 2007 Jun 11, 2007

Copy link to clipboard

Copied

Thank you again Adam :)

structKeyExists(myxmldoc["my-list"]["my-item"], "user-id")

This worked..

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
New Here ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

I have a similar question. Here's my xml structure:

<Response>
<Service>
<Name>Overnight</Name>
<Charge>30</Charge>
</Service>
<Service>
<Name>TwoDay</Name>
<Charge>15</Charge>
</Service>
</Response>

How do I test whether the 'Overnight' service is available? I tried StructKeyExists(xmlObj["Response"]["Service"]["Name"],"Overnight") but it doesn't work.

Thanks.

Min

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
Guest
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

does it have multiple entries and needs an array element reference?

i.e if(StructKeyExists(xmlObj["Response"][index]["Service"]["Name"],"Overnight")){
//do something?
}

if not do a <cfdump var="#xmlobj#"> and post the result

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
New Here ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

StructKeyExists(xmlObj["Response"]["Service"]["Name"],"Overnight") returns false. The trouble is "Overnight" is not a key of the structure. In fact, when I do StructKeyList(xmlObj["Response"]["Service"]["Name"]), it returns me an empty list.

So how can I check whether "Overnight" exists as one of the service options?

Thanks,
Min

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
Guest
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

is your xmlobj a string or an actualy xml structure? have you done a cfdump to confirm, and parsed it with xmlparse or some other function?

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
New Here ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

Yes, I can confirm that xmlObj is an xml object. I used xmlParse to create it and checked it with cfdump.

Regards,
Min

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
New Here ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

Ok, I think I figured out a way. I am not sure if this is the best and most efficient way, but it seems to work:

<cfset overnightSearch = xmlSearch(xmlObj, "Response/Service[Name ='Overnight']")>

to check that overnight service is available:

<cfif arrayLen(overnightSearch) gt 0>

to get the cost of overnight service:

xmlOvernight = xmlParse(overnightSearch)
cost = xmlOvernight["Charge"][1].xmlText

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
New Here ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

Sorry, slight correction.

To get the cost of overnight Service:
xmlOvernight = xmlParse(overnightSearch[1])
cost = xmlOvernight["Service"]["Charge"].xmlText

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
Advocate ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

You hit on the answer in an earlier post - "Overnight" is not a key for the structure - its the value of the key "Name". To test for it, you need to 1) verify that the element Name exists, and then 2) test to see if the value of the <name> element is "Overnight". Since "AND" in CF is a short circuit AND operator (operand? I never get my nominclature right), you can combine the tests into 1 CF statement:

<cfif structKeyExists(xmlObj["Response"]["Service"], "Name") AND xmlObj["Response"]["Service"]["Name"].xmlText eq "Overnight">

Do Something

</cfif>

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
New Here ,
Jun 14, 2007 Jun 14, 2007

Copy link to clipboard

Copied

LATEST
The problem with <cfif xmlObj["Response"]["Service"]["Name"].xmlText eq "Overnight"> is it doesn't work for "TwoDay" since it is not the first child.

Regards,
Min

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