Return to Snippet

Revision: 24011
at February 18, 2010 12:24 by BoNzO


Initial Code
/ 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, 'pattern.xml' );

Initial URL
http://www.as3dtk.com/?p=459

Initial Description
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 en


d of the previous version.   I’ve located the bug report here but can find no solutions, work arounds or additional information.

Initial Title
Open XML, modify and save on local

Initial Tags
Flex

Initial Language
ActionScript 3