User Guide

Table Of Contents
868 Chapter 35: Using XML and WDDX
FROM myquery
WHERE lname LIKE 'A%'
</cfquery>
<cfdump var=#imqtest#>
Converting a query object to XML
The following example shows how to convert a query object to XML. It uses
cfquery to get a list
of employees from the cfdocexamples database and saves the information as an XML document.
<!--- Query the database and get the names in the employee table --->
<cfquery name="myQuery" datasource="cfdocexamples">
SELECT FirstName, LastName
FROM employee
</cfquery>
<!--- Create an XML document object containing the data --->
<cfxml variable="mydoc">
<employee>
<cfoutput query="myQuery">
<name>
<first>#FirstName#</first>
<last>#LastName#</last>
</name>
</cfoutput>
</employee>
</cfxml>
<!--- dump the resulting XML document object --->
<cfdump var=#mydoc#>
<!--- Write the XML to a file --->
<cffile action="write" file="C:\inetpub\wwwroot\xml\employee.xml"
output=#toString(mydoc)#>
Validating XML documents
ColdFusion MX provides the following methods for validating a document against a DTD or an
XML Schema:
The XmlParse function can validate XML text that it is parsing against a DTD or Schema. It
the function encounters a validation error, ColdFusion generates an error and stops parsing the
text. If the validator generates warnings, but no errors, ColdFusion parses the document and
returns the result.
The XmlValidate function can validate an XML text document or XML document object.
against a DTD or Schema. The function returns a data structure with detailed information
from the validator, including arrays of warning, error, and fatal error messages, and a Boolean
status variable indicating whether the document is valid. Your application can examine the
status information and determine how to handle it further.
For examples of XML validation, see
XmlParse and XmlValidate in CFML Reference. The
XmlParse example validates a document using a DTD. The XmlValidate example validates the
document using an XML Schema that represents the same document structure as the DTD.