Search code examples
xmlactionscript-3dictionaryvalue-objectsrobotlegs

populating and accessing data from a value object


I have have a problem loading and accessing data from a value object in my new project.. I load an xml file via a service, which contains title and locations of asset files, I need to be able to access the location of an asset file by specifying the title and retrieiving it from a value object.. I'm using the Robotlegs framework, here's an example of the xml:

<?xml version="1.0" encoding="utf-8" ?>
<files id ="xmlroot">
<file title="css_shell"                 location = "css/shell.css"  />
<file title="xml_shell"                 location = "xml/shell.xml"  />
<file title="test"                      location=   "test/location/test.jpg" />
<file title ="shell_background_image"   location = "images/shell_images/background_image.jpg" />
</files>

I then push this data into a value object as a Dictionary hash.. hopefully

//-----------  populate value objects ------------------------------------
var xml:XML = new XML(xml);
var files:XMLList = xml.files.file;

for each (var file:XML in files) {
var filePathVO:DataVO = new FilePathVO( [email protected](),
     file.location.toString()
);

 filePathModel.locationList.push(filePathVO);
filePathModel.locationHash[filePathVO.title] = filePathVO;
 }

I've tested accessing this from a view component.

// accessing from another class -----------------

var _background_image_path:String = String( filePathModel.locationHash['shell_background_image']);

it returns undefined.. any ideas?


Solution

  • You forgot the @ for location attribute on this line:

    var filePathVO:DataVO = new FilePathVO([email protected](),
         file.location.toString());
    

    The real problem lies on the line:

    var files:XMLList = xml.files.file;
    

    Change it to

    var files:XMLList = xml.file;
    

    The xml variable refers to the root tag of the xml; there is no need to access it explicitly with xml.files. xml.files.file actually looks for file tags that are direct children of files tags that are direct children of the root-xml tag. It would've worked had your xml been something like:

    <?xml version="1.0" encoding="utf-8" ?>
    <root>
      <files id ="xmlroot">
        <file title="css_shell" location = "css/shell.css"  />
        <file title="xml_shell" location = "xml/shell.xml"  />
        <file title="test" location=   "test/location/test.jpg" />
        <file title ="shell_background_image" location = "images/shell_images/background_image.jpg" />
      </files>
    </root>