How to create XML from Flash?

I know you can import an XML file to supply Flash with data but is it possible to create an XML file from data held in variable/ array in Flash to be saved locally or on a web server ?

If so how would one do such a thing…how are the nodes and children created etc ?

:thumb:

i believe you can construct the xml, however, saving it would be an issue. I’m really not sure if you can save date into a file from flash… But… i believe you can use a server, and send the text to the server where an PHP or an ASP.Net or ASP app could save that data…
I’d have to look into it more. =) im intrigued.

in the meantime, you can look at this.

http://www.kirupa.com/developer/actionscript/xmlfromflash.htm

I see what you mean…If I were for example to create from scratch many records in an array consisting of the following field/variables:

Name
Address
Age
Phone
Interests

How would I create the structured XML file ?..I wonder if senocular could help on this one given that he wrote the tutorial.

I know you can load XML from a local file or web server…I do not want to load as I have the data in flash I want to create and save an xml file locally or to a web server.

This would be really useful for others I am sure…senocular are you around ???

well you would create the XML inflash, and there are methods that support that, and then you send that object or text (whatever it is…) to a webserver in which you’d have a web application running waiting to accept that file and write it out and save it.

I could make an example for you tonight, hopefully be done by tomarrow =)

I welcome your ethusiasm thanks…so what you are saying for example is :

[AS]function person(name, address, tel, interest) {
this.name = name;
this.address = address;
this.tel = tel;
this.interest = interest;
}[/AS]

[AS]people = [];
people.push(new person(“somebody”,“right here”,12345678,“drinking”));
people.push(new person(“somebody2”,“right here2”,00000000,“drinking & dancing”));[/AS]

This array somehow gets turned into:

<people>
      <person>
            <name>somebody</name>
            <address>right here</address>
            <tel>12345678</tel>
            <interests>drinking</interests>
      </person>
      <person>
            <name>somebody2</name>
            <address>right here2</address>
            <tel>00000000</tel>
            <interests>drinking & dancing</interests>
      </person>
</people>

I know this is not PHP but how do I post XML tags in this forum ????


How would the array get turned in the XML structure as above ??..

Could the xml above then be saved into a text file but with an .xml extension ???

I also need the chioce to save this locally or to a web sever for retrieval at a later date.

well to save thats what the web application (not flash) would be responsible for. (Im an ASP.net guy btw)

you’d do something like this… (this is just an example, i havent checked for errors etc)…

(taken from http://www.kirupa.com/developer/actionscript/xmlinflash.htm)

{Edited:}
im working on a demo for ya…
take a look at this…
Sen is the man =)

http://www.kirupa.com/developer/actionscript/create_edit_xml.htm

thats the idea, then you’d take the object and send that somehow =)

hold on thats not totally correct, lemme fix it…

ok i got it creating the xml correctly… heres what i have…




function person(name, address, tel, interest) {
        this.name = name;
        this.address = address;
        this.tel = tel;
        this.interest = interest;
}

people = [];
people.push(new person("somebody","right here",12345678,"drinking"));
people.push(new person("somebody2","right here2",00000000,"drinking & dancing"));


my_xml = new XML("<people />");

for(i=0;i < people.length;i++)
{
	my_xml.childNodes[0].appendChild(my_xml.createElement("person"));
	my_xml.childNodes[0].childNodes*.appendChild(my_xml.createElement("name"));
	my_xml.childNodes[0].childNodes*.childNodes[0].appendChild(my_xml.createTextNode(people*.name));
	my_xml.childNodes[0].childNodes*.appendChild(my_xml.createElement("address"));
	my_xml.childNodes[0].childNodes*.childNodes[1].appendChild(my_xml.createTextNode(people*.address));
	my_xml.childNodes[0].childNodes*.appendChild(my_xml.createElement("tel"));
	my_xml.childNodes[0].childNodes*.childNodes[2].appendChild(my_xml.createTextNode(people*.tel));
	my_xml.childNodes[0].childNodes*.appendChild(my_xml.createElement("interest"));
	my_xml.childNodes[0].childNodes*.childNodes[3].appendChild(my_xml.createTextNode(people*.interest));
}



trace(my_xml.toString());



it puts the xml in one line but its well formed =)

and no matter how many items you add to the people array it’ll still write out the entry for every person.

im working on the “saving” part… that will be an ASP.net web app. Not sure what server side programming you’re going to use, but i’ll try to help .

You say you got the XML now albeit on one line, brilliant !!!

Wonder if there is a way to nest the xml code ???

But now SURELY !!! using the loadvars() thingy it would be possible to save it to a text file locally with the extension .xml ie people.xml ???

Given that Sen and I share the same birthday 27th September :toad:…I think he should get involved at some point to help out.

Could you attach the .FLA

Bye for now…

:phil:

hehe

yea i’ll attach the FLA tonight. (but thats code is all there is =) )

what i did was use the Xml.Send(“url.aspx”) and it sends it using the post method, and from there i can take the info and write a file. =)

what server side scripting are you using?

I would be using PHP for storage of the XML file on a webserver but also want to store it on the c:\ locally.

What about this example to save locally:

[AS]
on (release) {
var1 = “some text here”;
var2 = “some text here 2”;
var3 = “some text here 3”;
fscommand (“save”, “example.xml”);
}
[/AS]

Obviously would need xml structure…

:slight_smile:

The FSCommand save only works with projector files and would need a c++, java, vb program to accept variables to create an XML file…bummer!!!

BUT how about sharedobject…YES !!!

http://www.kirupa.com/developer/mx/sharedobjects.htm

Now in theory I do not need to put data in an XML file format and save locally then retrieve at another time.

We could put the data in the shared object and then save it locally…with a load button to read the data from the shared object ???

What about this example:

A .swf contains a circle which starts at x,y 100,100 and three buttons, save, load and clear.

The circle is dragged to x,y 200,200 and then when save button is clicked that the circle and new co-ordinates are saved ie 200,200.

The clear button is clicked which starts the .swf from the first frame.

Then the load button is clicked which loads the shared object containing the circle and 200,200. ie the last position the circle was in.

Do you think this would work ???

well php has functionality (or should ) to write files locally. What you’ll neeed to do is “send” the data to a page, that page will read the variable (using the post method) and then you’ll take that string of text and output it to a local file.

Its been a long time i used php…

you’re on the right track though =)

Did you say you were going to try something with PHP ??

I could…

Let me see what i can do, im pretty busy as it is =) (stupid project at work)…

anyway, i’ll see what i can do for ya =)

Sounds great…priorities first though…I am doing some work with Ahmed at the moment this may have an impact on the XML thing.

Take it easy…