2 Replies Latest reply on Aug 25, 2008 7:42 PM by Ratsnackbar

    Dynamic Flex from XML

    topdecker981
      Hello,

      So i have quite an interesting flex application to build and I'm not sure where to start. Basically this application needs to take an XML file that will look similar to this:
      <registrationCampaign id=1>
      <page id = 10 number = 1>
      <template id = 1>
      <attribute type='formField' label ='username' />
      <attribute type='formField' label ='password' />
      <attribute type='image' source='url to the image' />
      </template id = 1>
      <template id =2>
      <attribute type='formField' label ='username' />
      <attribute type='formField' label ='password' />
      <attribute type='image' source='url to the image' />
      </template>

      </page>
      <page id = 10 number = 2>
      <template id = 1>
      <attribute type='formField' label ='username' />
      <attribute type='formField' label ='password' />
      <attribute type='image' source='url to the image' />
      </template id = 1>
      <template id =2>
      <attribute type='formField' label ='username' />
      <attribute type='formField' label ='password' />
      <attribute type='image' source='url to the image' />
      </template>
      </page>
      </registrationCampaign>


      That's not perfect but the basic idea. You have an xml file that has the information to build out a registration path. The pages are easy, just add another object to a view stack for each page and increment them by 1 on submits, it's the templates i'm worried about. Is there any way to dynamically type a a variable like var objec:{dynamic from xml} = new {dynamic from XML}? Not that sysntax obviously but i'm just wondering what's possible.

      The only other solution i've though of is limiting the types of things that can be dynamically added, then building a custom class that takes a type attribute and essentially creates the object as whatever is passed in. It would take some hard coding up front but i'm not convinced that's the best way. Any and all suggestions are welcome!

      -Toby
        • 1. Re: Dynamic Flex from XML
          Dr. Fred Mbogo Level 1
          Flex has its MXML language, but I don't believe you can load interfaces at runtime like that. They're translated into ActionScript code during compilation, so for it to work the same way, the MXML to AS compiler would have to ship with Flex apps. That doesn't seem likely to me.

          You might have the freedom to rebuild the application when the XML changes. You might not even know you have this freedom yet. :) (Kind of like being in the Matrix and not knowing it.) If so, you can translate your XML schema into MXML using any of several tools: XSLT, custom code, whatever. Then you can build the app and serve it. Flex comes with an Apache module for doing this, so even if you have to upload the XML and serve the new app immediately, this would still let you do that. You'd basically have an unchanging engine, the XML to MXML translator, and then the Flex Apache module to assemble the whole thing and serve it.

          If you can't rebuild the application, it wouldn't be too hard to slurp the XML into a running program, walk the resulting tree and translate the tags found into ActionScript object creation. Flex does let you build user interfaces on the fly, and the strong support for automatic layout makes the a lot less painful than you might initially imagine.
          • 2. Re: Dynamic Flex from XML
            Ratsnackbar Level 2
            Yes actually you can dynamically type variables in Flash / Flex using something similar to this.

            var myDynamicObject:* = whatEverObjectIFeelLikePuttingHere;

            Perhaps what might help to accomplish your goal is to use reflection inside of Flex. It is possible to use (although somewhat irritating) reflection in Flex using a form similar to this.

            var content:Class = getClassFromPath(objectClassPath);
            newComponent = new content();
            addChild(newComponent);

            The only problem is that the compiler will scrub any variables/classes that are not currently being referenced, at least once, somewhere in the application. So somewhere in your application you must have a reference for the class, even if it set to null, so that the class will not be removed from the project.

            It's also possible that the describeType function might be of some use.

            Here are two references that describe each.
            http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectID=580
            http://weblogs.thekeunster.com/?p=10

            Utilizing these features it's possible to do quite a bit to reshape your application based on data stored outside your application or on parameters only known at runtime.