Inserting a Flex data model into the MXML file

You can manually insert a Flex data model in an MXML file or you can visually import it.

To manually insert a data model into an MXML file:

  1. Open the file in Code view (View > Code).
  2. Write an <mx:Model> tag.

    For example, the following data model stores a person's name, age, and phone number:

    <mx:Model id="myEmployee">
    	<name>
    		<first>John</first>
    		<last>Doe</last>
    	</name>
    	<department>Accounting</department>
    	<email>jdoe@goodcompany.com</email>
    </mx:Model>
    

    For more information, see "Using data models" in Developing Flex Applications Help.

  3. Save the file.
  4. Use the Data panel (Window > Data) to inspect the data model.



    Note: If the data model doesn't appear in the Data panel, click the refresh button on the panel.

To visually import a data model into an MXML file:

  1. Open the file in Design or Code view.
  2. In the Data panel (Window > Data), click the Plus (+) button and select Model from the pop-up menu.
  3. In the dialog that appears, enter the relative path and filename of the XML file, or click the Browse button and select the XML file.

    This specifies the location of an XML file to use as a data model in the current MXML file.

    After you click OK, Flex Builder inserts the data model in the file.

  4. Use the Data panel to inspect the data model.

You could also hand-code an <mx:Model> tag with a source property that specifies an XML file or a URL that returns XML. The following are some examples:

<mx:Model source="content.xml" id="contact"/>
<mx:Model source="http://www.somesite.com/content.xml" id="company"/>

Note: If you create a data model with the source property, you will be unable to visually create a binding to the model because the binding destination is in an external file.