Save XML in flash

This article shows how to create XML and store it into a Local Machine without any dialog box.
In order to avoid the dialog box and save with full security, Adobe Air is the best option. FileStream from Adobe Air has the reliability to save the file without any user interaction

Step 1. Create one Sample XML file in C:\test.xml with <sample></sample>

Step 2: Create two textboxes and one save button with instances name uid , pwd and saveBtn

Step 3. The code below

import flash.filesystem.*;
var LoadXML=new URLLoader(new URLRequest(“c://test.xml”)) // Loading XML file
LoadXML.addEventListener(Event.COMPLETE,LoadXMLFunction) // on load of XML calls LoadXMLFunction
saveBtn.addEventListener(MouseEvent.CLICK,onSave) // on save button click calls onSave
var MainXML:XML;function LoadXMLFunction(e:Event)
{
MainXML=XML(e.target.data) //Load all XML data from test.xml to MainXML
trace(MainXML)
}
function onSave(e:Event):void
{
var item:XML = <citem /> // Create one XML Node
item.userId = uid.text // assign uid textbox value to userId node
item.pwd = pwd.text  // assign pwd textbox value to pwd node
MainXML.appendChild(item); // Append this xml item to MainXML 

var file:File = new File();
file = file.resolvePath(“c:\\test.xml”); // open test.xml 
var fileStream:FileStream = new FileStream(); // Create new FileStream to Perform Read or Write
fileStream.open(file, FileMode.WRITE); // Open file in WRITE Mode
fileStream.writeUTFBytes(MainXML);  //Write MainXML xml content to the file test.xml
fileStream.close(); //Close the File Stream
}

4 Replies to “Save XML in flash”

  1. It works perfect when i export it from flash. but its doesnt works when I run the swf or publish it as windows projector (.exe) from publish settings. Does oit works only

  2. Do you mind if I quote a few of your posts as long as I provide credit
    and sources back to your site? My website is in the very same
    area of interest as yours and my visitors would really benefit
    from some of the information you present here. Please let me know if this okay with you.
    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.