7 Replies Latest reply on Nov 4, 2011 10:05 AM by Owain North

    Colon in XML response

    pkonshak

      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?

        • 1. Re: Colon in XML response
          Owain North Level 4

          Should be

           

          #xmlfile["ns3:LookupMyAccountInfoResponse"]["ns3:numberOfCheckouts"].xmlText #

          • 2. Re: Colon in XML response
            pkonshak Level 1

            Thank you, thank you!  Seems so easy, but I just couldn't get it -- makes total sense now.  Much appreciated,

             

            Peter

            • 3. Re: Colon in XML response
              Owain North Level 4

              No worries, it's a Friday afternoon and I had lots of more boring work to get out of.

              • 4. Re: Colon in XML response
                pkonshak Level 1

                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[a]"]["ns3:title"].xmlT ext#<br>

                  

                </cfloop>

                • 5. Re: Colon in XML response
                  Owain North Level 4

                  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[i] />

                   

                       <cfdump var="#thisElement#">

                   

                       <cfloop from="1" to="#arrayLen(thisElement.xmlChildren#" index="j">

                   

                            <cfset thisInnerElement = thisElement.xmlChildren[j] />

                   

                            <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.

                  • 6. Re: Colon in XML response
                    pkonshak Level 1

                    Thanks again.  I'm still learning how to work with XML, so your response is most helpful.

                     

                    Peter

                    • 7. Re: Colon in XML response
                      Owain North Level 4

                      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