Object ExampleAnythingToXML will use an object's defined property names to create the XML nodes. Customer.cfc <cfcomponent>
<!--- customer object for object to XML example. --->
<cfproperty name="customerid" type="string" />
<cfproperty name="Firstname" type="string" />
<cfproperty name="Lastname" type="string" />
<cffunction name="init" access="public" returntype="any">
<cfargument name="Firstname" type="string" required="no" default="John">
<cfargument name="Lastname" type="string" required="no" default="Doe">
<cfset this.Firstname=arguments.Firstname />
<cfset this.Lastname=arguments.Lastname />
<cfset this.customerid= createUUID() />
<cfreturn this>
</cffunction>
</cfcomponent>
Code: <cfprocessingdirective suppresswhitespace="yes" >
<cfsetting enablecfoutputonly="yes">
<!--- Object to XML example. --->
<!--- Anything To XML will use the object's defined propery names to create the XML nodes --->
<!--- create an object --->
<cfset Customer = createObject('component', 'customer').init("Bob","Jones") />
<!---<cfdump var="#Customer#" > <cfabort>--->
<!--- create a list of attributes (optional)--->
<cfset AttributeList = "customerid" />
<cfset AnythingToXML = createObject('component', 'AnythingToXML.AnythingToXML').init() />
<cfset myXML = AnythingToXML.toXML(Customer,"CUSTOMER",AttributeList) />
<cfoutput>#myXML#</cfoutput>
</cfprocessingdirective>
Output: <CUSTOMER customerid="02E36DB8-1422-2598-1FDA367E36A7B95A">
<Lastname>Jones</Lastname>
<Firstname>Bob</Firstname>
</CUSTOMER>
|