0

I am pretty new to AS, and I am assuming there is a way to do this and I am just not figuring it out. Basically, I am trying to use a service that returns xml and return an Object regardless of the structure of the xml. In .Net I use the XmlSerializer.Deserialize class... is there equivalent in AS?

I was able to find SimpleXMLDecoder but I can't seem to get it to work - it also looks like it might only work with nodes? Either way, the examples out there are sparse and hard to follow, I just want to know how to take xml like this:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Company>
    <Id>2</Id>
    <Name>Stan</Name>
    <Size>10</Size>
</Company>; 

And simply turn it into an Object - is this possible without writing my own parser? Thank you.

2 Answers 2

5

ActionScript has its own XML parser then you don't need to write yours.

XML from a String

If you have a String to convert, you can just convert it as XML inline with few lines of code like this:


import flash.xml.*;

var xml : XML = XML( '<?xml version="1.0" encoding="utf-8"?><Company><Id>2</Id><Name>Stan</Name><Size>10</Size></Company>' );

trace( 'Id:' + xml.Id ); // Should trace "2"
trace( 'Name:' + xml.Name ); // Should trace "Stan"

XML from an external file

Otherwise you can just load it in runtime in this way:


import flash.net.*;
import flash.events.*;
import flash.xml.*;

var xmlLoader : URLLoader = new URLLoader();
xmlLoader.addEventListener( Event.COMPLETE, doStuffWithLoadedXML );

function doStuffWithLoadedXML( e : Event ) : void 
{                             
    var xml : XML = new XML( e.target.data );
    trace( 'Id:' + xml.Id ); // Should trace "2"
    trace( 'Name:' + xml.Name ); // Should trace "Stan"
}

xmlLoader.load( new URLRequest( 'yourfile.xml' ) );

Edited with links

Some nice links to start working:

The Basic
http://blog.theflashblog.com/?p=242

Some nice E4X tips and how-to
http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4

Hope this helps. Ciao!

Sign up to request clarification or add additional context in comments.

2 Comments

I wish I could mark two answers, this is correct too - I was just using services, so that worked out easier. Thank you!
It's ok man. That's the rule of the game and @Slomojo comes first ;-) Good luck!
3

You can use the HTTPService

There's a good example here...

Basically it will serialize the result into an object from XML when you retrieve it.

3 Comments

Hello, the HTTPService works fine but needs the Flex SDK that is not available if you use only the Flash ide.
It's available, you can just get the SDK, since it's free. However, if you just have an xml string and don't need the other features of HTTPService, just do var x:XML = XML(string);
wow... that is super easy, I should have asked this question earlier!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.