CFLib.org – Common Function Library Project

xmlDoctoString(xmlDoc)

Last updated August 2, 2003

author

Massimo Foti

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
Converts an CF XML objects to string without the XML declaration. In order to understand the need to have such a UDF you have to remember that CF always return the XML declaration when you turn an XML Doc to a string.

Return Values:
Returns a string.

Example:

<!--- Generate the XML doc --->
<cfsavecontent variable="xmlString">
	<geeks_list>
		<geek city="Washinton D.C." name="Tom Muck">The only guy I know able to write a whole book using more than 6 different server-side languages</geek>
		<geek city="Lugano (Switzerland)" name="Massimo Foti">The man who learned JavaScript in order to avoid learning german</geek>
		<geek city="Los Angeles" name="Angela Buraglia">By far the cutiest geek in town</geek>
		<geek city="Seattle" name="Dan Short">Be careful with him, he married a trained killer...</geek>
	</geeks_list>	
</cfsavecontent>

<cfoutput>#htmlEditFormat(xmlDoctoString(xmlString))#</cfoutput>

<!--- 
In order to understand the need to have such a UDF try the CFML code below. 
Basically CF always return the XML declaration when you turn an XML  Doc to string:
--->
<cfset XMLDoc=XmlParse(xmlString, "yes")>
<cfoutput>#htmlEditFormat(ToString(XMLDoc))#</cfoutput>

Parameters:

Name Description Required
xmlDoc Either a XML document or string. Yes

Full UDF Source:

<!---
 Converts an CF XML objects to string without the XML declaration.
 
 @param xmlDoc 	 Either a XML document or string. (Required)
 @return Returns a string. 
 @author Massimo Foti (massimo@massimocorner.com) 
 @version 1, August 2, 2003 
--->
<cffunction name="xmlDoctoString" output="no" returntype="string" displayname="xmlDoctoString" hint="Extract the root element inside an XML Doc and return it as a string">
	<cfargument name="xmlDoc" type="string" required="true" displayname="xmlDoc" hint="An XML Doc or a well formed XML string">
	<cfset var xmlToParse="">
	<!--- Check to see if the argument is already an XMLDoc --->
	<cfif IsXmlDoc(arguments.xmlDoc)>
		<cfset xmlToParse=arguments.xmlDoc>
	<cfelse>
		<!--- We need a parsed XML doc, not just a simple string --->
		<cftry>
			<cfset xmlToParse=XmlParse(arguments.xmlDoc, "yes")>
			<!--- Failed parsing, the string culd be not a well formed XML, throw an exception --->
			<cfcatch type="Any">
				<cfthrow message="xmlDoctoString: failed to parse argument.xmlDoc" type="xmlDoctoString">
			</cfcatch>
		</cftry>
	</cfif>
	<cfreturn xmlToParse.getDocumentElement().toString()>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson