Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web service invoked in a CFC problem - newbee question

Status
Not open for further replies.

pommoz

Programmer
Nov 12, 2002
3
AU
I have a webservice that I invoke on the page using a CFC, from the cfc I <cfreturn var=&quot;xmlContent&quot;>
XmlContent being <cfset XmlContent = xmlParse(XmlContent)> the content of the xml is passed in this variable to the page that invokes the component, then on that page I am currently extracting the information from the xml variable to show results on the screen:

<cfset xmlTitleNode = XmlContent.xmlroot.XmlText.XmlText>
<cfset numberOfItems = #ArrayLen(xmlTitleNode)#>

<cfloop from=&quot;1&quot; to=&quot;#numberOfItems#&quot; index=&quot;i&quot;>
<cfset 1stItem = #xmlTitleNode.XmlAttributes.1stItem#>

<cfoutput>
#1stItem#
</cfoutput>

</cfloop>

This works very well for each page that needs results from the CFC, but I would like to add these to a CFC so I don't need to set them up on each output page:

#get1stItem()#

Is this possible? Am a little green around the gills when it comes to passing values back from CFC's so would appreciate some help here please. Am using CFMX 6.1

Thanks
MW
 
Hmmmm... not quite sure what you're asking.

You can have as many functions in a webservice/CFC as you care to. One function could certainly return XMLcontent, and another could return the 1stItem.

Use CFOBJECT to instantiate the webserver, and CFINVOKE to call each method/function you need to from that webservice object.
Code:
   <CFOBJECT webservice=&quot;[URL unfurl="true"]http://myserver/mywebservice.cfm?wsdl&quot;[/URL] name=&quot;ws_Service&quot;>
   <CFINVOKE webservice=&quot;#ws_Service#&quot; method=&quot;GetXMLContent&quot; unparsedXML=&quot;#unparsedXML#&quot; returnVariable=&quot;XmlContent&quot;>
   <CFINVOKE webservice=&quot;#ws_Service#&quot; method=&quot;GetFirstItemArray&quot; XmlContent=&quot;#XmlContent#&quot; returnVariable=&quot;aryFirstItems&quot;>

   <CFLOOP from=&quot;1&quot; to=&quot;#ArrayLen(aryFirstItems)#&quot; index=&quot;whichElement&quot;>
       <CFOUTPUT>#whichElement#</CFOUTPUT><br />

   </CFLOOP>

Does that answer your question at all??



-Carl
 
Hi

Thanks for that, it was usefull. The point i was really getting at is The web services I use are obviously returned as xml, below is a snippet of one of the functions within the cfc:

<cffunction name=&quot;getPriceQuotes&quot; returntype=&quot;string&quot; output=&quot;false&quot; access=&quot;public&quot; hint=&quot;Invokes the login web service and passes the Sessionkey to the getPriceQuote web Service and returns the XML related to the stock&quot;>

<cfargument name=&quot;userName&quot; type=&quot;string&quot; required=&quot;true&quot;>
<cfargument name=&quot;password&quot; type=&quot;string&quot; required=&quot;true&quot;>
<cfargument name=&quot;securityList&quot; required=&quot;true&quot; type=&quot;string&quot;>
<cfinvoke webservice=&quot;URL&quot; method=&quot;createSessionKey&quot; returnvariable=&quot;SessionKey&quot;>
<cfinvokeargument name=&quot;userName&quot; value=&quot;#arguments.userName#&quot;/>
<cfinvokeargument name=&quot;password&quot; value=&quot;#arguments.password#&quot;/>
</cfinvoke>

<cfinvoke
webservice=&quot;URL&quot;
method=&quot;getPriceQuotesByList&quot;
returnvariable=&quot;XmlContent&quot;>
<cfinvokeargument name=&quot;sessionKey&quot; value=&quot;#sessionKey#&quot;/>
<cfinvokeargument name=&quot;securitiesList&quot; value=&quot;#arguments.securityList#&quot;/>
<cfinvokeargument name=&quot;options&quot; value=&quot;0&quot;/>
</cfinvoke>
<cfset XmlContent = xmlParse(XmlContent)>
<cfreturn XmlContent>
</cffunction>

So as you can see the quote xml is passed to the page that is calling it. Then on that page there are quite a number of <CFSET> functions to grab the specific information from this xml. (as shown in the previous post):

<cfset xmlTitleNode = XmlContent.xmlroot.XmlText.XmlText>
<cfset numberOfItems = #ArrayLen(xmlTitleNode)#>

<cfloop from=&quot;1&quot; to=&quot;#numberOfItems#&quot; index=&quot;i&quot;>
<cfset 1stItem = #xmlTitleNode.XmlAttributes.1stItem#>

<cfoutput>
#1stItem#
</cfoutput>

</cfloop>

What I would really like to do is pass those nodes from the xml through a CFC (as a method?) so there is not so much code on the page, as I said not a guru in cfc's so am a bit unsure, is that any clearer? :)

Many thanks

MW
 
Ummm... isn't that in my example?

In my example, I was invoking &quot;GetXMLContent&quot; to return the XML content (this would be akin to your &quot;getPriceQuotesByList&quot;). Then I send that XML that GetXMLContent returned and pass it into an invocation of &quot;GetFirstItemArray&quot;, which returns an array, or whatever other data structure you need... or it could even return completely formatted output, which you would then just need to wrap in CFOUTPUT tags and you're done (though, the disadvantage to this would be that you're not separating the data layer from the presentation layer... which reduces the flexibility of the function).


-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top