Targeting XMLSchema subtypes for anonymous ActionScript Objects

When encoding ActionScript objects to XML using a schema, such as when using a SOAP based WebService, it is not uncommon to provide a subtype (say, an Employee class) to an API that merely specifies a base type (say, a Person class). An issue is that the encoder does not know the subtype that your value is targeting so you must provide a hint. For the decoding XML to ActionScript case, an xsi:type attribute provides the decoder with such a hint… so an equivalent is needed in ActionScript for the encoding XML case.

For strongly typed objects, you can implement the mx.rpc.xml.IXMLSchemaInstance interface to provide a QName for the xsiType property to target the subclass to be used during encoding. See:

For anonymous ActionScript Objects, however, you may not know that you can wrap your object in an mx.utils.ObjectProxy instance and set its type property with a QName to achieve the same effect. See:

Using the following XML Schema as an example…

We may want to provide an Employee instance as a Record’s person value, but unless we tell the encoder we have something more than a Person instance, the Employee-specific properties will not be encoded.JohnSmith

To avoid having to create Person or Employee strong types, here’s a snippet showing how to create an ObjectProxy wrapper to instruct the encoder to encode an anonymous Object as an Employee:

import mx.utils.object_proxy;import mx.utils.ObjectProxy;...var emp:Object = {firstName:"John", lastName:"Smith", employeeId:1000};var employee:ObjectProxy = new ObjectProxy(emp);var employeeType:QName = new QName("urn:MyWebService", "Employee");employee.object_proxy::type = employeeType;

Now the encoder should know to include the Employee specific employeeId property:JohnSmith1000

The source for a complete example is attached as a file to download.

One Response to Targeting XMLSchema subtypes for anonymous ActionScript Objects

  1. Frank Young says:

    I’m trying to utilize a SOAP webservice that uses abstract complexTypes that require the xsi:type to be set on some of the request elements.I’ve tried implementing the IXMLSchemaInstance for my classes, but to no avail.I’ve gotten the service to work just fine sending and receiving simple types, but these abstract complex types are giving me a headache.I’ve found a few blog posts about this issue but nothing has solved my issues yet.I’m wondering if it might make more sense to have our services team generate a WSDL specifically for Flex clients – I’ve been bangin my head on this stuff for a week now.I would love to hear from someone who has alot of experience dealing with complex WSDL’s in Flex.Thanks.