View Full Version : Using XML to populate controls.

08-12-2006, 07:44 PM
Hi all,

I am using an XML file called "ProductOptions.xml" which provides the label and data information for a series of List based controls I am using in my Flex 2 Application. Basically, I load the XML file and each node (and it's children) represent an option in a given list.

Currently, my plan is to load in the XML file and then loop over each node, build it into an ArrayCollection and bind it to the Component I wish to populate.

I wonder though if there may be a faster way to do this.

See my code below. One is for the Class doing the parsing and the other is the XML source file. Could someone be kind enough to review these and let me know if you think there may be a faster method.

Actionscript 3.0 Class

package com.desktopae.vo
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import mx.collections.ArrayCollection;
import flash.xml.XMLNode;
import flash.events.EventDispatcher;

public class ProductOptions extends EventDispatcher{
private var optionsXML:XML;

public var occupancyTypes:ArrayCollection;

public function ProductOptions(){

public function getXMLData():void{
var XMLURL:URLRequest = new URLRequest('xml/ProductOptions.xml');
var xLoader:URLLoader = new URLLoader(XMLURL);
xLoader.addEventListener(Event.COMPLETE, optionsLoaded);

private function optionsLoaded(event:Event):void{
optionsXML = XML(event.target.data);

var op:XMLList = XMLList(optionsXML.valuelist.(@basedOn == 'ProductOccupancyTypes'));

occupancyTypes = new ArrayCollection();

for each(var item in op.children()){
occupancyTypes.addItem({label: item.toString(), data: item.@id});

var e:Event = new Event(Event.COMPLETE);


XML Document

<?xml version="1.0" encoding="iso-8859-1"?>

<valuelist basedOn="ProductMortgageHistory">
<value id="1">6M Cancelled Checks</value>
<value id="2">12M Cancelled Checks</value>
<value id="3">FNMA VOM</value>
<value id="4">FNMA VOR</value>
<value id="5>Private VOM</value>
<valuelist basedOn="ProductTypes">
<value id="1">Option Arm</value>
<value id="2">1M Arm</value>
<value id="3">6M Arm</value>
<value id="4">1YR Arm/value>
<value id="5">2YR Fixed</value>
<value id="6">3YR Fixed</value>
<value id="7">5YR Fixed/value>
<value id="8">7YR Fixed</value>
<value id="9">10YR Fixed</value>
<value id="10">15YR Fixed</value>
<value id="11">20YR Fixed</value>
<value id="12">30YR Fixed</value>
<value id="13">40YR Fixed</value>
<value id="14">30 Due 15</value>
<value id="15">40 Due 30</value>
<valuelist basedOn="ProductAmoritizationTypes">
<value id="1">Full AM</value>
<value id="2">Neg. AM</value>
<value id="3">Interest Only</value>
<value id="4">Reverse Mortgage</value>
<valuelist basedOn="ProductSourceOfDown">
<value id="1">Cash on Hand</value>
<value id="2">Gift of Equity</value>
<value id="3">Checking/Savings</value>
<value id="4">Cash Deposit/Escrow</value>
<value id="5">Equity/Property Sold</value>
<value id="6">Equity/Subject Property</value>
<value id="7">Equity/Pending</value>
<value id="8">Stock Bond Cash Out</value>
<value id="9">Lease Option Converzion</value>
<value id="10">Cash Borrowed</value>
<valuelist basedOn="ProductAssets">
<value id="1">Verified Assets</value>
<value id="2">Stated Assets</value>
<value id="3">No Assets</value>
<valuelist basedOn="ProductEmploymentStatus">
<value id="1">Self Employed</value>
<value id="2">Wage Earner</value>
<value id="3">Retired</value>
<valuelist basedOn="ProductTaxDocTypes">
<value id="1">W2</value>
<value id="2">1099</value>
<value id="3">CPA Letter</value>
<value id="4">Full Tax Returns</value>
<valuelist basedOn="ProductIncomeDocumentation">
<value id="1">Full Doc</value>
<value id="2">Lite Doc</value>
<value id="3">Stated Income</value>
<value id="4">No Ratio</value>
<value id="5">No Income</value>
<value id="6">No Doc</value>
<value id="7">Full or Alt/Lite Doc, 6M Personal Bank Statements</value>
<value id="8">Full or Alt/Lite Doc, 6M Business Bank Statements</value>
<value id="9">Full or Alt/Lite Doc, 12M Personal Bank Statements</value>
<value id="10">Full or Alt/Lite Doc, 12M Business Bank Statements</value>
<value id="11">Full or Alt/Lite Doc, 24M Business Bank Statements</value>
<value id="12">Full or Alt/Lite Doc, 24M Business Bank Statements</value>
<valuelist basedOn="ProductOccupancyTypes">
<value id="1">Owner</value>
<value id="2">Non-Owner</value>
<value id="3">2nd Home/Vacation</value>

I am loading only the last node right now for example's sake, but in the final implementation each node would need to be loaded and bound to a component.

Any ideas or will the current loop plans I have not slow down the application too much? My concern is that although it could work now, it may not scale very gracefully.

Thanks so much.

08-13-2006, 08:03 PM
Ok, well I am half way there although not overly pleased with the methods presented. I can easily parse the XML and then simply drill down on the nodes I want and assign them (via mx:Model tag) to an ArrayCollection, which by default, is the perfect data provider for List based controls.

Here's the rub.

If I prefer to do all my loading from Class files, and mx:Model is an MXML only function, then how do I separate logic from presentation??

And frankly, in a technology that boasts MVC patterns, what the hell is a tag called mx:Model which manipulates data models, doing belonging only to the View classes? Meaning the MXML components?