Specifying an external source for an <mx:Model> tag or <mx:XML> tag

You can specify an external source for an <mx:Model> or <mx:XML> tag in a source property. Separating the content of a model from the MXML that defines the user interface improves the maintainability and reusability of an application. Adobe recommends this way of adding static XML content to a Flex application.

The external source file can contain static data and data binding expressions, just like a model defined in the body of the <mx:Model> or <mx:XML> tag. The file referenced in a source property resides on the server and not on the client machine. The compiler reads the source value and compiles the source into the application; the source value is not read at run time. To retrieve XML data at run time, you can use the <mx:HTTPService> tag; for more information, see Using RPC Components.

Using <mx:Model> and <mx:XML> tags with external sources is an easy way to reuse data model structures and data binding expressions. You can also use them to prepopulate user interface controls with static data by binding data from the model elements into the user interface controls.

The source property accepts the names of files relative to the current web application directory, as well as URLs with HTTP:// prefixes. In the following example, the content of the myEmployee1 data model is an XML file named content.xml in the local web application directory. The content of the myEmployee2 data model is a fictional HTTP URL that returns XML.

<mx:Model source="employees.xml" id="employee1"/>

<mx:Model source="http://www.somesitel.com/employees.xml" id="employee2"/>

The source file must be a valid XML document with a single root node. The following example shows an XML file that could be used as the source of the <mx:Model source="employees.xml" id="Model1"/> tag. <

<?xml version="1.0"?>
<employees>
    <employee>
        <name>John Doe</name>
        <phone>555-777-66555</phone>
        <email>jdoe@fictitious.com</email>
        <active>true</active>
    </employee>
    <employee>
        <name>Jane Doe</name>
        <phone>555-777-66555</phone>
        <email>jndoe@fictitious.com</email>
        <active>true</active>
    </employee>
</employees>

Flex 2.01

Take a survey