Friday, November 14, 2008

Generating dynamic XML file in FLEX(client side)

Well this is another area where i found very few resources .
may be because people would have thought it as so simple that it doesnt even require a help or support for anyone to build one....
But..i think i should answer the call now...
one may require at any point of time to generate a file or some sort of temporary data store, what more can it be other than XML????

the next important thing that matters is where it need to be generated, serverside or Client side?
if it is client side we need to do it in AS3 and is much much easier than on serverside...which is in AS1

what we do on in AS3 to generate in AS3 is:

import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.controls.*;
import flash.filesystem.*;

[Bindable]
private var _file:File;
[Bindable]
private var _fileStream:FileStream;
[Bindable]
private var writeString:String="";


private function Radioalerts():void
{
if(rad1.selected) //rad1 is the id of the radio button i created

{writeString="&ltrad1>"+rad1.value+"\n";//simple concatenation of tag.}

if(rad2.selected) //rad2 is the id of the radio button i created

{writeString="&ltrad2>"+rad2.value+"\n";}

if(rad3.selected) //rad3 is the id of the radio button i created

{writeString= "&ltrad3>"+rad3.value+"\n"; }
}

private function writeXml():void
{

Radioalerts();

_file = File.documentsDirectory.resolvePath("XML Folder/New-xml.xml");

_fileStream = new FileStream();

writeString= writeString+writeString1+writeString2;

writeString = "&ltroot>"+writeString+"\n";

savefile();

}

private function savefile():void

{

_fileStream.open(_file,FileMode.WRITE);

_fileStream.writeUTFBytes(writeString);

_fileStream.close();
}

Well the basic idea is that you get the details that u want to be in the xml file of yours as a string and append the tags (simple concatenation) and write it to the file as u write a text file....thats it folks..!!!YOU HAVE GOT YOUR XML FILE in your documents directory .."My Documents/XML Folder/New-xml.xml"...

No comments:

Post a Comment