Download the EasyCFM.COM Browser Toolbar!
Easy XML creation in 2 minutes
Untitled Document

Easy XML creation

Hi dude, how are you?

Today I will be talking about something that lot's of people think important, but don't know how to create it well.

We gonna talk about XML!

A fewer time ago it was necessary in one of my applications, 'cause this content would be used by another system that could not access my database to take the information. After think a lot I've seen that the better way to do it was line by line. A terrible error, 'cause 3 or 4 dais ago I knew a Custom Tag that could do exactly the same.
Then... My way was the hardest

Well, but now we will not talk about this custom tag, in my case I had to take 87 fields of the database and put it in variables like it:

<cfset name="<name>#query.name</name>#">

After this I've take all this variables and concatenated to create a file with CFFILE.
Veeeery Tiring work not?

Affecter the Cold Fusion MX, this happily will not have more than to happen, because now we have the tag CFXML that do exactly the creation of XML documents from a query or other.

This tag is so simple, and when I discovered it I wanted to die!!!!
Now I' will show a simple example how to create a xml document from a query.

Firs we have to create a table in our database who could have the next fields:

idUser Auto-Number
nameUser Text
emailUser Text
cityUser Text

Then we want to generate an XML with the data from the database.

Our structure have to be like this:

<Users>
      <iduser>
      <name>
      <email>
      <city>

</Users>

Starting our code:

Lets make a query that return the data.

<cfquery datasource="article" name="users">
SELECT *
FROM Users
</cfquery>

Now we have to use the tag CFXML

//Our tag receives the params
<cfxml variable="xmlUsers">

     //Our table
     <Users>
          <cfoutput query="users">
               //Here is our fields
               <user id="#users.idUser#">
               <name>#users.nameUser#</name>
               <email>#users.emailUser#</email>
               <city>#users.
cityUser#</city>
               </user>

          </cfoutput>
     </Users>
</cfxml>

Then we have to create a variable that will transform our XML content to string to move the data without errors.

<cfset xml = #ToString(xmlUsers)#>
//This variable receive the parameter passed as variable in the tag CFXML

Then, now we only have to create our xml file.

<cffile action="write" file="#getDirectoryFromPath(getTemplatePath())#users.xml" output="#xml#">

Ok, now we already have the xml file ready to be used. Like this:

<?xml version="1.0" encoding="UTF-8" ?>
- <Users>
      - <user id="1">
           <name>Marcos Placona</name>
           <email>i_netmaster@hotmail.com</email>
           <city>Sao Paulo</city>

        </user>
      - <user id="2">
           <name>Chrystian Adams</name>
           <email>chrystian@yahoo.com</email>
           <city>Boston</city>

        </user>
</users>

Right now you can generate your own XML.

Questions, comments...mail-me

Thanks for your attention,

Marcos Placoná

 



All ColdFusion Tutorials By Author: Marcos Placoná