First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing! | Flash Jobs
Search tutorials
Author: Matthias Davis | Website: http://www.multidmedia.com

Converting the CSV Data into XML Data

The function convertToXml will do the CSV to XML conversion. In this function, first the user is asked where they would like to save their XML file using the Browse Dialog we used in the openCSV function. The first 8 lines of this function do this:

mdm.Dialogs.BrowseFile.filterList = "XML file|*.xml";
mdm.Dialogs.BrowseFile.title = "Save XML file";
mdm.Dialogs.BrowseFile.buttonText = "Save";
if (System.capabilities.os.substr(0, 3).toLowerCase() == "win") {
var xmlPath = mdm.Dialogs.BrowseFile.show();
} else {
var xmlPath = mdm.Dialogs.BrowseFileToSave.show();
}

Here, the Browse Dialogs is customised in the same way we customised it before. The 4th line uses a, 'if' statement to determine whether the projector is being run on Windows or the Mac and displays the relevant Browse Dialog (The Mac version of Zinc has a dedicated SaveAs Dialog that we can use, mdm.Dialogs.BrowseFileToSave.show).

The following code will convert the CSV data into XML Data and save that to a file specified by the user :

var myXML:XML = new XML();
var myDatabase:XMLNode = myXML.createElement("myDatabase");
var theDB:Array = csvData.split("n");
for (n=0; n var Record:XMLNode = myXML.createElement("Record"+[n+1]);
if (delim.value.toString() == "{tab}") {
var Fields:Array = theDB[n].split("t");
} else {
var Fields:Array = theDB[n].split(delim.value.toString());
}
for (i=0; i var childNode:XMLNode = myXML.createElement("Element"+[i+1]);
var childTextNode:XMLNode = myXML.createTextNode(Fields[i]);
childNode.appendChild(childTextNode);
Record.appendChild(childNode);
}
myDatabase.appendChild(Record);
}
myXML.appendChild(myDatabase);
var XMLdata = myXML.toString();
if (xmlPath.toLowerCase().lastIndexOf(".xml") == xmlPath.length-4) {
mdm.FileSystem.saveFile(xmlPath, XMLdata);
} else {
mdm.FileSystem.saveFile(xmlPath+".xml", XMLdata);
}
}


Here's a line by line explaination :

var myXML:XML = new XML();
var myDatabase:XMLNode = myXML.createElement("myDatabase");
var theDB:Array = csvData.split("n");
for (n=0; n var Record:XMLNode = myXML.createElement("Record"+[n+1]);


First of all we need to create an XML object
Then we create our 'root' node (myDatabase) which will contain the whole database
An array 'theDB' is created which holds the database (each array value will hold a record)
A 'for' loop is started which will loop through each array value (record in our database)
'Record' nodes are created which will hold all the data for each record

if (delim.value.toString() == "{tab}") {
var Fields:Array = theDB[n].split("t");
} else {
var Fields:Array = theDB[n].split(delim.value.toString());
}


Although CSV files normally use commas as field delimiters, some database managing programs may allow you to export CSV data and use either comma, semicolon, tabs and other characters as delimeters. In our example, code to handle commas, semicolons and tabs as delimeters has been included in these 5 lines (delim.value.toString() refers to the combobox in the example fla which allows the user to select which delimeter our program should look out for).

for (i=0; i var childNode:XMLNode = myXML.createElement("Element"+[i+1]);
var childmyXML:XMLNode = myXML.createTextNode(Fields[i]);


A 'for' loop is started which will loop through each field
'Element' nodes are created which will hold the data for each field
TextNodes are created which will contain the actual data for each field

childNode.appendChild(childmyXML);
Record.appendChild(childNode);
}
myDatabase.appendChild(Record);
}
myXML.appendChild(myDatabase);


Our TextNode is inserted into our 'Element'
Our 'Element' is inserted into our 'Record'
When every field has been added to its respective record we then add that record to our 'root' node
When every record has been added to the root node we add all that data to our XML object

var XMLdata = myXML.toString();

As XML is simply text data, we need to convert our XML object to String data by using 'myXML.toString()' so that it can be saved correctly as an XML file.

if (xmlPath.toLowerCase().lastIndexOf(".xml") == xmlPath.length-4) {
mdm.FileSystem.saveFile(xmlPath, XMLdata);
} else {
mdm.FileSystem.saveFile(xmlPath+".xml", XMLdata);
}


Then you can save it using the FileSystem.saveFile Zinc command (Note: this code ensures that whatever the user specifies as the output file name, the resultant output file is always an XML file);

And there you have it! You can use this code to generate XML files from any CSV file!

» Level Intermediate

Added: 2006-11-17
Rating: 8 Votes: 2
(10 being the highest)
» Author
I am a student of the University of Wolverhampton working in Technical Support at Multidmedia Limited the creators of MDM Zinc v2.5 which you can see is advertised on the left of this page.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.