Reputation: 161
Here is a SOAP request example:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ws="http://ws_test">
<soapenv:Header/>
<soapenv:Body>
<ws:testService a1="a1" a2="a2">
<ws:e1>e1</ws:e1>
<ws:e2>e2</ws:e2>
</ws:testService>
</soapenv:Body>
</soapenv:Envelope>
And here is my example cfc web service:
<cfcomponent style="document" wsversion = 1>
<cffunction name="testService" returntype="String" access="remote" >
<cfargument type="string" name="e1"/>
<cfargument type="string" name="e2"/>
<!--- Missing: code to extract a1 and a2 --->
<cfreturn "#e1# #e2#">
</cffunction>
</cfcomponent>
I'm new to Coldfusion and web service, and I have no idea on how to extract attributes a1 and a2 from <testService>
, googled it but can't find any reference. Any ideas?
=== Edit ===
Thought it might be useful if I attach the type definition:
<complexType name="testServiceType">
<sequence>
<element name="e1" type="string"></element>
<element name="e2" type="string"></element>
</sequence>
<attribute name="a1" type="string"/>
<attribute name="a2" type="string"/>
</complexType>
Note that although this is my test web service, but it is based on a data schema that is provided by our partner, which means my web service has to conform with it.
=== Resolution ===
Based on Gerry's answer, this is what I ended up doing:
<cfcomponent style="document" wsversion = 1>
<cffunction name="testService" returntype="String" access="remote" >
<cfargument type="string" name="e1"/>
<cfargument type="string" name="e2"/>
<cfset soapReq = getSOAPRequest()>
<cfset soapXML = xmlParse(soapReq)>
<cfset attributes = soapXML.Envelope.body.XmlChildren[1].XmlAttributes>
<cfset a1 = attributes.a1>
<cfset a2 = attributes.a2>
<cfreturn "#e1# #e2# #a1# #a2#">
</cffunction>
</cfcomponent>
Upvotes: 4
Views: 209
Reputation: 936
Based on your comment, I think you need getSoapRequest() and then parse it using the code from the answer given by keshav-jha
<cfcomponent style="document" wsversion = 1>
<cffunction name="testService" returntype="String" access="remote" >
<cfargument type="string" name="e1"/>
<cfargument type="string" name="e2"/>
<cfscript>
soapReq=GetSOAPRequest();
soapXML=xmlParse(soapReq);
bodyAttributes = {
a1:soapXML.Envelope.body.XmlChildren[1].XmlAttributes.a1
,a2:soapXML.Envelope.body.XmlChildren[1].XmlAttributes.a2
};
return serializejson(bodyAttributes);
</cfscript>
</cffunction>
</cfcomponent>
Upvotes: 1
Reputation: 936
If you are creating the web service, then you have complete control over how the webservice is consumed. In this case a1 and a2 are not ever passed into the CFC for processing. So if they have meaning, you can set them up as parameters like e1 and e2.
One of the best tools I've ever used for understanding and using web services is SoapUI. If you create the example.cfc and point SOAPUI at it, you will get an XML request that looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.stack">
<soapenv:Header/>
<soapenv:Body>
<soap:testService>
<soap:e1>e1</soap:e1>
<soap:e2>e2</soap:e2>
</soap:testService>
</soapenv:Body>
</soapenv:Envelope>
If you want to process a1 and a2, there is no reason not to handle them as regular arguments.
So you could make a CFC that looks like this:
<cfcomponent style="document" wsversion = 1>
<cffunction name="testService" returntype="String" access="remote" >
<cfargument type="string" name="a1"/>
<cfargument type="string" name="a2"/>
<cfargument type="string" name="e1"/>
<cfargument type="string" name="e2"/>
<cfset var ret=serializeJSON(arguments) />
<cfreturn "#ret#">
</cffunction>
</cfcomponent>
And if you point SOAPUI at it, then it will generate a SOAP envelope that looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.stack">
<soapenv:Header/>
<soapenv:Body>
<soap:testService>
<soap:a1>?</soap:a1>
<soap:a2>?</soap:a2>
<soap:e1>?</soap:e1>
<soap:e2>?</soap:e2>
</soap:testService>
</soapenv:Body>
</soapenv:Envelope>
Upvotes: 0
Reputation: 1364
You just need to parse your XML and then get the value of a1 and a2
<cfsavecontent variable="myXML" >
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws_test">
<soapenv:Header/>
<soapenv:Body>
<ws:testService a1="a1" a2="a2">
<ws:e1>e1</ws:e1>
<ws:e2>e2</ws:e2>
</ws:testService>
</soapenv:Body>
</soapenv:Envelope>
</cfsavecontent>
<cfset parXML = xmlParse(myXML) />
<cfdump var="#parXML.Envelope.body.XmlChildren[1].XmlAttributes.a1#">
Upvotes: 1