This post examines a method for saving a modified XML file to disk. To accomplish this task, the XML is first converted to binary data using byteArray – and then saving the byteArray to disk using fileReference.
The ByteArray class is part of the flash.utils package and supports the reading and writing of binary data. The FileReference class is part of the flash.net package and is used to upload and download files from a local computer to a server.
In the code below, a XML file named personnel has a record appended to it and then is converted to a binary file using ByteArray and saved to disk with FileReference. The first time changes are made and saved, a new file is created on disk reflecting the changes made – this works fine in both Windows and Mac systems. Now the caveat; when the XML is modified a second time and saved, the code in Windows overwrites the previous version as expected – but in Mac OS10, the original XML is not overwritten but creates a new version of the file with the new version appended to the end of the previous version. I’ve located the bug report here but can find no solutions, work arounds or additional information.
// xml personnel file var personnel:XML = <employees> <employee id='1187' lastName='Davis' firstName='Charles'> <office>CT</office> <dept>Marketing</dept> </employee> <employee id='3383' lastName='Jones' firstName='Kevin'> <office>NY</office> <dept>Sales</dept> </employee> <employee id='2946' lastName='Samuels' firstName='Elizabeth'> <office>CT</office> <dept>Engineering</dept> </employee> </employees> // new node appended personnel.appendChild( <employee id='4002' lastName='Suzuki' firstName='Kenji'> <office>MA</office> <dept>Sales</dept> </employee> ); // convert xml to binary data var ba:ByteArray = new ByteArray( ); ba.writeUTFBytes( personnel ); // save to disk var fr:FileReference = new FileReference( ); fr.save( ba, 'personnel.xml' ); |
If you have any further information on the bug mentioned above, please let us know.
Check this out:
http://flexbuddha.posterous.com/xmlfileloader-
with-filestream
i have 3 klasses that make my life easier