4 Replies Latest reply: Aug 15, 2012 5:33 PM by p.sim RSS

    How to create a struct from an XML?

    Sudeep_Tester

      Hi,

       

      I am a QA and need to create a struct from an XML which is generated from a POST in the front end. I have fetched the XML generated using firebug. I need to pass the struct to a variable and use it for testing purpose.

       

      Please can anyone help?

        • 1. Re: How to create a struct from an XML?
          Miguel-F Community Member

          ColdFusion makes this very easy for you.  Simply make a cfhttp call to retrieve your xml document.

           

           

          <cfhttp url="url.to.your.xml" method="post" timeout="10">

                    <!--- include any necessary <cfhttpparam /> tags here --->

          </cfhttp>

           

           

          That call will store the xml document in a variable for you; named "cfhttp" unless you specify otherwise.

           

           

          Next I like to check for success before trying to parse the xml.  Here is some pseudo code for you.

           

           

          <cfif find("200",cfhttp.StatusCode)>  <!--- check that the http call itself was successful --->

                    <cfif IsXml(cfhttp.FileContent)>  <!--- check that the response is valid xml format --->

              <cfset soapResponse=xmlParse(cfhttp.FileContent) />  <!--- Convert xml text into an xml document object --->

              <cfdump var="#soapResponse#" />  <!--- Dump the xml document object --->

              <!--- At this point you have an xml object that you can then use things like xmlsearch on (and other xml functions) --->

              <!--- xmlsearch uses an XPath language expression to search an XML document object --->

                    <cfelse>

              <!--- response was not xml --->

                    </cfif>

          <cfelse>

                    <!--- http call failed --->

          </cfif>

           

           

          Hope that helps.

          • 2. Re: How to create a struct from an XML?
            Sudeep_Tester

            Hi Miguel,

             

            Thanks for the info. The complete command collection XML generated was as below.

             

            <commandcollection class="array"><command><target>SocialMediaChannel</target><method>save</method><args><id> 0</id><name>Ch06</name><description>Ch06</description><serviceID>2</serviceID><publicURL>https://www.twitter.com/TestPthinTest</publicURL><propertiesData><CommentCheckInterval>10</CommentCheckInterval></propertiesDat a></args></command></commandcollection>

             

            I need to convert this to a struct and pass this as a variable. Actually the field where it would be passed as a variable will check for the "CommentCheckInterval" which is a part of the above XML.

             

            Please can you tell me how woould I convert the above xml to a struct?

            • 3. Re: How to create a struct from an XML?
              Miguel-F Community Member

              Ok, as I mentioned before now that you have your xml document object you can perform other xml functions on that.  If you now perform an xmlSearch on your object it will parse it into a structure for you.  So adding to the code I gave you before, replace the comment referencing xmlsearch with something like:

               

              <cfset CommandResponse=xmlSearch(soapResponse,"//*[name()='command']") />

              <cfdump var="#CommandResponse#" />

               

              See what that gives you and you should be able to go from there.