PDA

View Full Version : Is it possible to add Data into XML directly from flex?



1somniac
September 14th, 2007, 11:16 AM
Hi,


I am actually testing the XML tutorial from adobe (http://www.adobe.com/devnet/flex/quickstart/using_data_providers/) and I would like to add data directly in the XML file.

Here is the xml file :

Model (bloggers.xml)

<bloggers>
<blogger>
<name>Andy Budd</name>
<url>http://andybudd.com</url>
</blogger>

<blogger>
<name>Grant Skinner</name>
<url>http://gskinner.com</url>
</blogger>
<blogger>

<name>Paul Booth</name>
<url>http://paulbooth.com</url>
</blogger>
</bloggers>

And here the main application file :



<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
viewSourceURL="src/DataProviderExternal/index.html"

width="350" height="220"
creationComplete="bs.send();"
>
<mx:Script>

<![CDATA[
import mx.managers.CursorManager;
import mx.rpc.events.InvokeEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;

[Bindable]

private var bloggersCol:ArrayCollection;

// Gets called when HTTPService is invoked to
// request the XML.
private function bsInvokeHandler(event:InvokeEvent):void

{
// Display the busy cursor
CursorManager.setBusyCursor();
}

// Gets called when the XML is successfully loaded.
private function bsResultHandler(event:ResultEvent):void

{
// Save a reference to the list of bloggers
bloggersCol = event.result.bloggers.blogger;

// Hide the busy cursor
CursorManager.removeBusyCursor();
}


private function bsFaultHandler(event:FaultEvent):void
{

// There was an error in loading the XML
Alert.show (event.fault.message);

// Hide the busy cursor
CursorManager.removeBusyCursor();
}
]]>

</mx:Script>

<!-- Service to load in XML -->
<mx:HTTPService
id="bs"
url="data/bloggers.xml"

invoke="bsInvokeHandler(event);"
result="bsResultHandler(event);"
fault="bsFaultHandler(event);"

/>

<mx:Panel title="Bloggers we love!" width="100%">

<mx:List
id="bloggersList" width="100%" rowCount="4"

dataProvider="{bloggersCol}"
labelField="name"
/>

<mx:ControlBar horizontalAlign="center">

<mx:Button
label="Add a blogger!"
click="bloggersCol.addItem({name:'Pete-Barr Watson', url:'http://petebarrwatson.com/'});"

/>
</mx:ControlBar>

</mx:Panel>
</mx:Application>


is there any possibility to make the data directly into the XML file without using a server ?

thank you

senocular
September 14th, 2007, 11:27 AM
You just want your XML file in Flex instead of requesting it from an external file?

1somniac
September 14th, 2007, 11:44 AM
I just want to write into an XML file, even if I can't request it from an external file

senocular
September 14th, 2007, 11:56 AM
ooo I misread. No, you need a server or some other backend to actually do the writing. Flash will not be able to do that on its own.

1somniac
September 14th, 2007, 12:08 PM
thank you for your answer, I was looking for a way to write data directly and I haven't found any possibility.

I'm now using AIR for this, should I use the FileStream.write method from AIR in order to have my XML file ?

senocular
September 14th, 2007, 12:09 PM
you can do it using AIR (for local files) - and write would be the way to

1somniac
September 14th, 2007, 12:21 PM
thank you, i'm going on it!