To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flex and AIR

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-23-2009, 11:23 AM   #1
halcyon32
Member
 
Join Date: Jan 2003
Location: Atlanta, USA
Posts: 30
FileMode.APPEND XML

Hi All,

I'm working on my first AIR1.5 app in Flash CS3/AS3. What I'm trying to do is "APPEND" an XML Node to the end of an XML doc.

Starting XML:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<galeria> 
	<Photo imagem="casinha.jpg" legenda="Minha casa"/> 
	<Photo imagem="familia.jpg" legenda="Família reunida"/>
</galeria>
Im able to write to the XML file but it adds the node to the end of the XML out side of the "<galeria></galeria> Node".
Code:
var fl:File = File.desktopDirectory.resolvePath("air/testFile.xml");
var fs:FileStream = new FileStream();
fs.addEventListener(Event.COMPLETE, processXMLData);
fs.openAsync(fl, FileMode.READ);
var xml:XML;
//

function processXMLData(e:Event):void {
	xml = XML(fs.readUTFBytes(fs.bytesAvailable));
	testtext.text = xml;
	writeXML();
}
function writeXML():void {
	//fs.openAsync(fl, FileMode.WRITE);
	fs.openAsync(fl, FileMode.APPEND);
	var xmlNew = '<Photo imagem="NodeToBeAdded.jpg" legenda="Node to be added" />';
	fs.writeUTFBytes(xmlNew);
	testtext.text = xml;
	fs.close();
}
So I end up with this XML:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<galeria> 
	<Photo imagem="casinha.jpg" legenda="Minha casa"/> 
	<Photo imagem="familia.jpg" legenda="Família reunida"/>
</galeria><Photo imagem="NodeToBeAdded.jpg" legenda="Node to be added" />
this is the XML I want:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<galeria> 
	<Photo imagem="casinha.jpg" legenda="Minha casa"/> 
	<Photo imagem="familia.jpg" legenda="Família reunida"/>
        <Photo imagem="NodeToBeAdded.jpg" legenda="Node to be added" />
</galeria>
If anyone has a tutorial of a sample code I would really appreciate it.

Thanks
Scott
Any help would be appreciated.
__________________
1152x870 is all I have to say.
halcyon32 is offline   Reply With Quote
Old 10-29-2009, 12:37 PM   #2
redjag
Member
 
Join Date: May 2009
Posts: 58
Well you'll have to change it to not use append mode and rewrite the entire file.

Code:
fs.openAsync(fl, FileMode.WRITE);
xml += '<Photo imagem="NodeToBeAdded.jpg" legenda="Node to be added" />';
fs.writeUTFBytes(xml);
fs.close();
You might be able to make it more efficient by seeking to the position in the file where you want to write and writing, but I'm not 100% sure how that'd work.
redjag is offline   Reply With Quote
Old 11-08-2009, 01:38 AM   #3
ImprisonedPride
Pumpkin Carving 2008
 
ImprisonedPride's Avatar
 
Join Date: Apr 2006
Location: Grand Rapids MI
Posts: 2,135
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.
__________________

| Windows 7 Professional | Ubuntu 9.10 | San Diego A8N32-SLI Deluxe | AMD64 4000+ OC 2.9GHz | 3GB DDR 3200 | 512MB nVidia 7600 GT | 512MB nVidia 7800 GTX | 4 Monitors |
| CS4 Actionscript 3.0 | Java | Javascript | C++ | C# | Visual Basic | ASP.net | HTML | XML | Perl | PHP | AJAX | MySQL |
| Working on a project? I'm available for freelance. |

Last edited by ImprisonedPride; 11-08-2009 at 02:30 AM.
ImprisonedPride is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flex and AIR

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:25 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.