export e4x as XML file

Copy link to clipboard
Copied
Hello,
First let me say that I am new to the forums and relatively new to Flash CS3 and ActionScript 3.0, but I have been spending the past few weeks wih tutorials of varying kind and quality, so I'm starting to get a decent handle on things. That being said, I have an issue that I cant seem to reconcile.
What I want to do is create a form, have the data from the form compile into XML using e4x syntax and then use PHP to save that compiled data in standard XML format so that another page can access the XML file. Thanks in advance.
Brooks
Copy link to clipboard
Copied
There are two parts to this I think.
First you want to get your 'form' data into an XML format.
Second, you want to send it to the server and have php do the rest.
Do you need help with both of these?
First e4x is just a method to represent XML in actionscript. PHP has different XML libraries to handle xml in its own way...so I'm not sure what you mean by 'standard XML'.Both actionscript and php handle correctly formatted XML, its just the way they do it (the way it is represented and manipulated in code) that is different.
Let's start with part 1 above.
I just quickly wrote what I think is a reasonably generic object to XML encoding function.
If you put your form variables as properties of the formVars object, you should be able to encode them into some descriptive XML using this function.
function xmlEncode(val:Object,to:XML=null):XML {
var ret:XML;
if (!to) {
ret= <encodedData/>;
} else {
ret=to;
}
var className:String = flash.utils.describeType(val).@name;
var node:XML;
switch (className) {
case "String" :
case "Boolean" :
case "Number" :
case "int" :
case "uint" :
ret.@type=className;
ret.appendChild(val.toString());
break;
case "Array" :
ret.@type=className;
for each (var item:Object in val) {
node = <arrayElement/>;
ret.appendChild(xmlEncode(item,node));
}
break;
case "Object" :
ret.@type=className;
for (var prop:String in val) {
node = XML("<"+prop+"/>");
ret.appendChild(xmlEncode(val[prop],node));
}
break;
default :
trace(className+" encoding not implemented");
break;
}return ret;
}//test object
var formVars:Object = new Object();
formVars.var1=true;
formVars.value1="my text string";
formVars.myNumber=3.1415;
formVars.list= [1,2,3,4,5];
trace('complex Object test');
var xmlToSend:XML = xmlEncode(formVars);
trace(xmlToSend.toXMLString());
trace('single Array test');
xmlToSend = xmlEncode(["hello",1,true]);
trace(xmlToSend.toXMLString());
trace('simple string test');
xmlToSend = xmlEncode("hello");
trace(xmlToSend.toXMLString());
trace('simple Number test');
xmlToSend = xmlEncode(1.2 );
trace(xmlToSend.toXMLString());
Let us know if this makes sense and/or if you need help with sending the xml to your php script.

Copy link to clipboard
Copied
Hey, thanks for the response, sorry I got tied up with a wedding and haven't had the chance to respond. So anyway, most of that makes sense to me, at least the general idea does. Basically here's what I am wanting to do. My friend is having me put together a simple HTML/JavaScript Photography Website for them, no biggie, but I want to set them up with a Flash app that will let them upload new photos to their site by adding form information and uploaded image location to an existing XML document, that the website will already be looking for. So as for the building the XML in flash, I think i've got it, I do need help making sure that I send it all out properly so that the website understands it. And if there is a better way to do all this, I am open to suggestions, part of me feels like Flash would be good for something like this when it is all said and done, but the other part just wants to do it in Flash to get back in the game. Anyway, thanks again.
Brooks
Copy link to clipboard
Copied
You could use Flex too.

Copy link to clipboard
Copied
I had thought of that, but I own Flash, and not Flex yet.. and I spent enough time as a kid ripping off great companies so I like to make use of what I've bought now. That being said, do you think there would be benefits to using Flex? I never really toyed around with Shockwave in the olden days nor do I know much about Flex other than it makes use of AS3. Thanks.
Brooks
Copy link to clipboard
Copied
The good thing about Flex, is that is has a lot of prebuilt components that
you can use right away. the Flex SDK is for free, and you could use
FlashDevelop that is also free, to code your apps. But Flex Builder will be
the right tool if you wanna drag and drop controls. And a Flex app once is
compiled becomes a swf most of the times, so it runs on top of the Flash
Player.
Copy link to clipboard
Copied
Flex is just another way to build flash applications. You get another (optional) way to code classes/components in xml (mxml) and the compiler does a few extra things for you that speeds up development (like binding between components etc).
In simple term's its a combination of an actionscript framework and an 'enhanced' compiler for making your swfs.
The Flex SDK is free and you can use it with the free FlashDevelop editor if you want to try it out at no cost. That's how I started.
Basically what Michael said lol
There are no 'rules' for choosing either plain flash over flex or vice versa, but I think generally that Flex is used more for RIA/application style sites and Flash is used more for interactive websites...and the line is kinda blurry between those definitions. Factors like swf size can come into play (although less of a concern now with flex as the player caches a lot of the flex components, which traditionally were quite a bit of extra download 'weight')

