Open XML, modify and save on local


/ Published in: ActionScript 3
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. / xml personnel file
  2. var personnel:XML = <employees>
  3. <employee id='1187' lastName='Davis' firstName='Charles'>
  4. <office>CT</office>
  5. <dept>Marketing</dept>
  6. </employee>
  7. <employee id='3383' lastName='Jones' firstName='Kevin'>
  8. <office>NY</office>
  9. <dept>Sales</dept>
  10. </employee>
  11. <employee id='2946' lastName='Samuels' firstName='Elizabeth'>
  12. <office>CT</office>
  13. <dept>Engineering</dept>
  14. </employee>
  15. </employees>
  16.  
  17. // new node appended
  18. personnel.appendChild( <employee id='4002' lastName='Suzuki' firstName='Kenji'>
  19. <office>MA</office>
  20. <dept>Sales</dept>
  21. </employee> );
  22.  
  23.  
  24. // convert xml to binary data
  25. var ba:ByteArray = new ByteArray( );
  26. ba.writeUTFBytes( personnel );
  27.  
  28. // save to disk
  29. var fr:FileReference = new FileReference( );
  30. fr.save( ba, 'pattern.xml' );

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.