poblar y acceder a los datos de un objeto de valor
-
01-10-2019 - |
Pregunta
tengo un problema de carga y acceso a los datos de un objeto de valor en mi nuevo proyecto .. me carga un archivo XML a través de un servicio, que contiene el título y la ubicación de archivos de activos, tengo que ser capaz de acceder a la ubicación de un archivo activo, especificando el título y retrieiving desde un objeto de valor .. estoy usando el marco Robotlegs, he aquí un ejemplo del 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>
a continuación, empuje estos datos en un objeto de valor como un hash Diccionario de esperar ..
//----------- 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( file.@title.toString(),
file.location.toString()
);
filePathModel.locationList.push(filePathVO);
filePathModel.locationHash[filePathVO.title] = filePathVO;
}
He probado el acceso a este a partir de un componente de vista.
// accessing from another class -----------------
var _background_image_path:String = String( filePathModel.locationHash['shell_background_image']);
devuelve undefined .. alguna idea?
Solución
Se olvidó la @
de atributo de ubicación en esta línea:
var filePathVO:DataVO = new FilePathVO(file.@title.toString(),
file.location.toString());
El verdadero problema está en la línea:
var files:XMLList = xml.files.file;
Cambiar a
var files:XMLList = xml.file;
La variable xml
se refiere a la etiqueta raíz de la xml; no hay necesidad de acceder a ella de forma explícita con xml.files
. xml.files.file
en realidad se ve para las etiquetas file
que son hijos directos de etiquetas files
que son hijos directos de la etiqueta raíz en XML. Hubiera trabajado había sido su XML algo como:
<?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>