View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfxml Previous

cfxml

Creates a ColdFusion XML document object that contains the markup in the tag body. This tag can include XML and CFML tags. ColdFusion processes the CFML code in the tag body, then assigns the resulting text to an XML document object variable.

Extensibility tags

<CFXML 
variable="xmlVarName"
caseSensitive="yes" or "no">

IsXmlDoc, IsXmlElem, IsXmlRoot, XmlChildPos, XmlNew, XmlParse, XmlSearch, XmlTransform

ColdFusion MX: Added this tag.

Attribute

Req/Opt

Default

Description

variable

 

 

Name of an xml variable

caseSensitive

Optional

no

  • yes: maintains the case of document elements and attributes
  • no

An XML document object is represented in ColdFusion as a structure.

The following example creates a document object whose root element is MyDoc. The object includes text that displays the value of the ColdFusion variable testVar. The code creates four nested child elements, which are generated by an indexed cfloop tag. The cfdump tag displays the XML document object.

Note: Do not include an <?xml ?> processing directive in the cfxml tag body. This directive is not required for processing, and causes an error. To process XML text that includes this directive, use the XmlParse function.

<cfset testVar = True>
<cfxml variable="MyDoc">
   <MyDoc>
      <cfif testVar IS True>
         <cfoutput>The value of testVar is True.</cfoutput>
      <cfelse>
         <cfoutput>The value of testVar is False.</cfoutput>
      </cfif>
      <cfloop index = "LoopCount" from = "1" to = "4">
         <childNode>
            This is Child node <cfoutput>#LoopCount#.</cfoutput>
         </childNode>
      </cfloop>
   </MyDoc>
</cfxml>
<cfdump var=#MyDoc#>


Contents > CFML Reference > ColdFusion Tags > cfxml Previous

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


alancole said on Aug 13, 2003 at 8:36 AM :
This code example does not work. It produces the following error: "Document root element is missing." Anyone know a work-around for the CFXML tag??
alancole said on Aug 13, 2003 at 8:58 AM :
Addendum to above comment. My file had a <cfsetting enablecfoutputonly="yes" showdebugoutput="no"> tag above the code example. This appears to cause the 'root element missing' error message - If I remove the cfsetting tag, then there is no error.
cliffyman said on Sep 27, 2003 at 5:40 PM :
Precisely, since you'd have to wrap the entire area inside the CFXML tag with CFOUTPUT since the CFSETTING you indicate will hide all output not inside a CFOUTPUT tag.
0034 said on Jan 29, 2004 at 1:02 AM :
<?xml version="1.0" encoding="UTF-8" ?>
how i can chage the encoding parameter?
like <?xml version="1.0" encoding="euc-kr" ?>
halL said on Jan 30, 2004 at 9:03 AM :
In ColdFusion MX 6.1, the cfxml tag converts the its body text into an internal XML document object, which is always stored in memory Unicode.
You use ToString to reconvert the document object to a text string, at which time ColdFusion automatically prepends the <?xml version="1.0" encoding="UTF-8" ?> XML declaration.
To change the declaration to specify another encoding, use the Replace function.
To specify the encoding of the text that is returned to a browser or other application, use the cfcontent tag.
The following example shows this use:

<cfprocessingdirective suppresswhitespace="Yes">
<cfcontent type="text/xml; charset=utf-16">
<cfxml variable="xmlobject">
<breakfast_menu>
<food>
<name quantity="50">Belgian Waffles</name>
<description>Our famous Belgian Waffles</description>
</food>
</breakfast_menu>
</cfxml>
<!--- <cfdump var="#xmlobject#">--->
<cfset myvar=toString(xmlobject)>
<cfset mynewvar=replace(myvar, "UTF-8", "utf-16")>
<!---<cfdump var="#mynewvar#">--->
<cfoutput>#mynewvar#</cfoutput>
</cfprocessingdirective>

(By the way, the cfprocessingdirective tag prevents ColdFusion from putting white space characters in front of the XML declaration.)
Simon Whatley said on Mar 26, 2004 at 2:12 AM :
I have the following code (see below). When I close the file in DWMX6.1 and reopen it again for editing, all the xml tags are stripped of their names! Can anyone tell me why? Many thanks.

<cfquery datasource="#request.ds#" name="getGamePack">
SELECT packid,welcome_1,welcome_2,welcome_3,rules_1,rules_2,title,category_a,category_b,category_c,category_d
FROM tbl_packData
WHERE packid = #form.packid#
</cfquery>

<cfquery datasource="#request.ds#" name="getGameCards">
SELECT packid,cardid,title,description,score_a,score_b,score_c,score_d,image_url
FROM tbl_cardData
WHERE packid = #form.packid#
</cfquery>

<cfxml variable="xmlTrumpsGame" casesensitive="yes">
<gamepack>
<cfoutput query="getGamePack">
<welcome1>#getGamePack.welcome_1#</welcome1>
<welcome2>#getGamePack.welcome_2#</welcome2>
<welcome3>#getGamePack.welcome_3#</welcome3>
<rules1>#getGamePack.rules_1#</rules1>
<rules2>#getGamePack.rules_2#</rules2>
<title>#getGamePack.title#</title>
<catA>#getGamePack.category_a#</catA>
<catB>#getGamePack.category_b#</catB>
<catC>#getGamePack.category_c#</catC>
<catD>#getGamePack.category_d#</catD>
</cfoutput>

<cfoutput query="getGameCards">
<card id="#getGameCards.cardid#">
<title>#getGameCards.title#</title>
<desc>#getGameCards.description#</desc>
<scoreA>#getGameCards.score_a#</scoreA>
<scoreB>#getGameCards.score_b#</scoreB>
<scoreC>#getGameCards.score_c#</scoreC>
<scoreD>#getGameCards.score_d#</scoreD>
<url>#getGameCards.image_url#</url>
</card>
</cfoutput>
</gamepack>
</cfxml>

<cfset xml = #ToString(xmlTrumpsGame)#>

<cffile action="write" file="#getDirectoryFromPath(getTemplatePath())##getGamePack.packid#.xml" output="#xml#">
jonwrob said on Jun 30, 2004 at 2:10 PM :
Is there a way for cfxml to escape illegal characters such as "&", "<", and ">" as the document is being parsed?

It appears that cfxml throws an error if any xml element contains an "&" character. What it should do is escape the character, that is, return "&amp;". The alternative to doing this automatically is to add a call to a replace function for every piece of data that is in the xml document. Needless to say, this is highly inconvenient.
frinky!!! said on Jul 14, 2004 at 5:21 AM :
Answer to jonwrob: you should use the XMLFormat() function, which is exactly made for this type of thing.
lukecrouch said on Nov 29, 2004 at 9:15 AM :
is there a way to have either the cfxml tag or the ToString function exclude markup for empty elements? so if an element is:

<PONumber></PONumber>

it won't be written out to the XML string at all?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-b21.htm