The MXMLLoader Class > Dynamic load MXML In Flex Builder 3

First of all I don’t really use flex to do flex stuff and I’m not a big fan of flex. But in my latest project I had to build with flex an interface witch would be always diferent (a dynamic interface). In the beginning we thought in parsing some type of XML, but if we are in flex why not parse MXML? I took a look over the internet and I only found MXMLLoader component v0.01, witch didn’t compile on flex builder 3 and was really in the beginning. So there it is, as XMMLLoader class for flex builder 3. You just have to instantiate the class with the mxml :

You can access objects by name:

Or by id:

And have access to component events like:

The class with an example:

mxml-loader.zip

Example swf

7 Responses to “The MXMLLoader Class > Dynamic load MXML In Flex Builder 3”

  1. Toby Beal Says:

    Thank you for this article. I’ve been playing around with your example as well as the original component that you mention and I seem to be stuck. Are you familiar with the following error and where I may have gone wrong?

    Thanks.

    Error #1009:

    [code]

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at MXMLLoader/createChildFromMXMLNode()[/Users/bealtr/Documents/Storage/Workspace/Flex Builder 3/MXML/src/MXMLLoader.as:111]
    at MXMLLoader()[/Users/bealtr/Documents/Storage/Workspace/Flex Builder 3/MXML/src/MXMLLoader.as:41]
    at MXML/init/onLoaded()[/Users/bealtr/Documents/Storage/Workspace/Flex Builder 3/MXML/src/MXML.mxml:25]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

    [/code]

    Here’s the code for my main mxml file. I’ve got both MXMLLoader.as and MXMLManifest.as in the source directory.

    [code]

    [/code]

  2. Toby Beal Says:

    Oops. I guess it filtered the code from my example is there a way for me to post you the code?

  3. Idoru Says:

    could you please post the files online so that we have a look?
    It would generally seem as accessing it too early, or a typo, but do post the code :)

  4. Ivan Valadares Says:
    This loader does not parse all types of components, I didn’t add all of them because flex 3 have a lot of component. Probably you are adding a component that I didn’t import the reference but there’s no problem with it, it’s simple to add. Like Pedro said please post a Url with a zip contain the example you are trying to run I will take a look.
  5. jwall Says:

    Is it possible to use DataGrid components? When trying I receive errors like:

    TypeError: Error #1034: Type Coercion failed: cannot convert mx.controls::DataGrid@151c7851 to mx.core.Container.

    and

    ReferenceError: Error #1065: Variable is not defined.

    What you are showing here seems very powerful!

  6. Ivan Valadares Says:
    There’s no problem in adding the DataGrid, you just have to declare a variable of type Datagrid eg.“private var dataGrid:DataGrid;” in the MXMLLoader Class, the problem is the “mx:columns” and “mx:DataGridColumn headerText=”Column 1″ dataField=”col1″” nodes, this is a problem because the class will try of instantiate a component of type columns and type DataGridColumn that doesn’t exists, like I said previous I didn’t try this class will all the components. The solution for this problem is to add a special case to see if the node if one of this types and then add the columns by code. If you need any help, e-mail me :)
  7. jwall Says:

    That is what I was thinking…thank you for the verification and thank you for sharing your insight and example.

Leave a Reply