A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: FileMode.APPEND XML

Threaded View

  1. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    I actually just had to do this for the first time last night. Your best bet is to use XML.appendChild() to the base XML, delete the xml file, and write the whole thing back to to file. Here's how I did it:

    Code:
    	
    	import flash.filesystem.*;
    	
    	public class XMLEditor {
    		
    		private var file:File = null;
    		private var fileStream:FileStream = null;
    		public var data:XML = null;
    		private var _drive:File;
    		private var _setdir:String = "C:\\test.xml";
    		
    		public function XMLEditor(d:File) {
    			_drive = d;
    			open();
    		}
    		
    		public function open() {
    			load();
    			var inXML = fileStream.readUTFBytes(fileStream.bytesAvailable);
    			if (inXML == "") {
    				fileStream.writeUTFBytes("<records>\r\n</records>");
    				fileStream.close();
    				load();
    			} else {
    				data = new XML(inXML);
    			}
    		}
    		
    		public function load() {
    			file = _drive.resolvePath(_setdir);
    			fileStream = new FileStream();
    			fileStream.open(file, FileMode.UPDATE);
    		}
    		
    		public function save() {
    			fileStream.close();
    			file.deleteFile();
    			load();
    			fileStream.writeUTFBytes(data.toXMLString());
    			fileStream.close();
    		}
    To test:
    Code:
    // resolve the base drive for the xml file first into the _drive var
    var _drives:Array = File.getRootDirectories();
    var _drive:File = null;
    
    for (var n = 0; n < _drives.length; n++) {
    	if (_drives[n].name == "C:") {
    		_drive = _drives[n];
    	break;
    	}
    }			
    var xmle:XMLEditor = new XMLEditor(_drive);
    
    var insertXML:XML = <item>
    <id>1</id>
    <name>Hello World!</name>
    </item>
    
    xmle.data.appendChild(insertXML);
    
    xmle.save();
    
    trace(xmle.data.item.(id=="1")[0].name); // Hello World!
    
    /*
       Untested from snippets of my current project
                                                                        */
    This is just off the top of my head and a few pieces of code from my project. If it doesn't work it's probably me writing off the top of my head since it works for me just fine. Hopefully it points you in the right direction.
    Last edited by ImprisonedPride; 11-08-2009 at 02:30 AM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center