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á

 

About This Tutorial
Author: Marcos Placoná
Skill Level: Advanced 
 
 
 
Platforms Tested: CFMX
Total Views: 113,041
Submission Date: March 06, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 4
Discuss This Tutorial
  • Thanks for making it very easy......

  • How Would i Be able to convert data from 3 tables into 1 XML Doc ??

  • Ooops. I had all etc tags still there... :P silly noob, thought i had already tried without them... Thanks for writing this. I'm gonna make use of it:) Cheers, Tristan

  • This is my code, i think everything is there: #users.nameUser# #users.emailUser# #users.cityUser# Any ideas?

  • Hi, thans for your comment, the problem is because you are forgeting the root, that needs to be "" Like this: //Here is our fields #users.nameUser# #users.emailUser# #users.cityUser# Regards and good luck. Marcos Placoná

  • I get this error when i try this script: Document root element is missing. Document root element is missing. The error occurred in D:\CFusionMX\wwwroot\xml\index.cfm: line 19 17 : #users.cityUser# 18 : 19 : 20 : 21 :

  • Thanks for the Nuggets of Info! It's the people who share are the one's that make it better! Let's not forget the convenience and the availability to the access of this information. Yes it would be nice to get more en-depth with the tutorial, but there is enough code here to start the basic structure that will allow others to expand and grow with. The most important part of this tutorial is not the code, but the ability to get the developer to meet difficult task with creative solutions. Coding the XML file by hand would sure be a pickel, but hay thanks to Marcos for showing us another way to look challanges that are presented in the day to day task of a CF developer. There is always anoter way to skin a cat! Thanks Marcos!

  • Thanks for the Nuggets of Info! It's the people who share are the one's that make it better! Let's not forget the convenience and the availability to the access of this information. Yes it would be nice to get more en-depth with the tutorial, but there is enough code here to start the basic structure that will allow others to expand and grow with. The most important part of this tutorial is not the code, but the ability to get the developer to meet difficult task with creative solutions. Coding the XML file by hand would sure be a pickel, but hay thanks to Marcos for showing us another way to look challanges that are presented in the day to day task of a CF developer. There is always anoter way to skin a cat! Thanks Marcos!

  • It's because I didn't have time to write the second, third, fouth, etc. lol But if you think you can, please, do your best man :o) Regards, Marcos Placoná

Advertisement


Website Designed and Developed by Pablo Varando.