Copy link to clipboard
Copied
Hi,
I'm having a devil of a time with this. I'm using cfhttp to perform a REST transaction, the returned XML being parsed with xmlparse and saved to a variable called "xmlfile".
The resulting xml has a colon in. When I try reference part of it, my variable would look like:
#xmlfile.ns3:LookupMyAccountInfoResponse.ns3:numberOfCheckouts.xmlText#
Of course this fails due to the colons..
I understand there's some sort of bracket notation that should allow this to work. No matter where I put the brackets, it still fails.
Help?
Should be
#xmlfile["ns3:LookupMyAccountInfoResponse"]["ns3:numberOfCheckouts"].xmlText #
Copy link to clipboard
Copied
Should be
#xmlfile["ns3:LookupMyAccountInfoResponse"]["ns3:numberOfCheckouts"].xmlText #
Copy link to clipboard
Copied
Thank you, thank you! Seems so easy, but I just couldn't get it -- makes total sense now. Much appreciated,
Peter
Copy link to clipboard
Copied
No worries, it's a Friday afternoon and I had lots of more boring work to get out of.
Copy link to clipboard
Copied
Well, I may have spoken too soon....or actually a slightly different question. Now I want to access an element. I'm looping over the result after counting the number of responses, but when I try and access the element using the loop index, it can't find it -- is this because I bracketed it?
<cfloop from="1" to="#numtitles#" index="a">
#xmlfile["ns3:LookupMyAccountInfoResponse"]["ns3:patronCheckoutInfo"]["ns3:title"].xmlText#<br>
</cfloop>
Copy link to clipboard
Copied
Break it down slowly, use single quotes if needed inside doubles and and reference the XmlChildren attribute to get child elements. Something like this:
<cfloop from="1" to="#arraylen(xmlfile['ns3:LookupMyAccountInfoResponse'].xmlChildren)#" index="i">
<cfset thisElement = xmlfile['ns3:LookupMyAccountInfoResponse'].xmlChildren />
<cfdump var="#thisElement#">
<cfloop from="1" to="#arrayLen(thisElement.xmlChildren#" index="j">
<cfset thisInnerElement = thisElement.xmlChildren
<cfdump var="#thisInnerElement#" />
</cfloop>
</cfloop>
Until you have everything you're after. The important thing to remember is that child elements are represented by an ARRAY by the name XmlChildren.
Copy link to clipboard
Copied
Thanks again. I'm still learning how to work with XML, so your response is most helpful.
Peter
Copy link to clipboard
Copied
No worries, it's a bit odd as with CF you never *really* deal with XML when reading - you tend to deal with XML that's been converted into CF structs and arrays. You basically need to completely forget that you're dealing with XML, and just deal with it as you would any CF structure.
Don't try and do too much in one go, frequently dump out your variables to check you have what you *think* you have, and you'll get the hang in no time