Tam
Tam

Reputation: 12042

How to parse XML from Rails in Flex

I want to upload a file(photo) from Flex to Rails and then send a response back to the server in XML (contains photo URL and ID). I'm sending from my Rails server some XML as follows:

render(:xml => {:id => @photo.id, 
                :photoURL => @photo.URL, 
                :thumbPhotoURL => @photo.thumbURL})

This is sent through FileReference object through fileReference.upload()

I try to render it in the complete handler:

fileReference.addEventListener(Event.COMPLETE,function(event:Event):void {
                    var xml:XML = new XML(event.target.data);
                    ......

it doesn't seem to parse XML properly. I have used similar code before with URLLoader and it worked. Any ideas?

Upvotes: 0

Views: 257

Answers (2)

Danny Kopping
Danny Kopping

Reputation: 5190

May i ask why you're converting the data to a ByteArray? URLLoader actually has a great property called dataFormat which you can use to specify the way that Flash will handle the loading. You can choose between binary, text or url-encoded variables.

Like Amarghosh said, you're probably better off using the URLLoader for working with XML.

Upvotes: 1

Amarghosh
Amarghosh

Reputation: 59451

FileReference is for transferring files between user's hard disk and the server - the upload() function is for sending a file from user's machine to the server.

Use URLLoader to load xml from the server to your flex application

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest(url));

function onLoad(e:Event):void
{
  var loadedText:String = URLLoader(e.target).data;
  trace(loadedText);
  var xml:XML = new XML(loadedText);
  trace(xml.toXMLString());
}

Upvotes: 1

Related Questions