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

How to create a struct from an XML?

New Here ,
Aug 08, 2012 Aug 08, 2012

Copy link to clipboard

Copied

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?

Views

976

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
Engaged ,
Aug 08, 2012 Aug 08, 2012

Copy link to clipboard

Copied

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.

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 ,
Aug 09, 2012 Aug 09, 2012

Copy link to clipboard

Copied

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></propertiesData></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?

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
Engaged ,
Aug 09, 2012 Aug 09, 2012

Copy link to clipboard

Copied

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.

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
Engaged ,
Aug 15, 2012 Aug 15, 2012

Copy link to clipboard

Copied

LATEST

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