3

我在新项目中从值对象加载和访问数据时遇到问题。我通过服务加载 xml 文件,其中包含资产文件的标题和位置,我需要能够访问资产文件的位置通过指定标题并从值对象中检索它。我正在使用 Robotlegs 框架,这是 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>

然后我将这些数据作为字典哈希推送到值对象中。希望

//-----------  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;
 }

我已经测试过从视图组件访问它。

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

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

它返回未定义..有什么想法吗?

4

1 回答 1

1

您忘记了@这一行的 for location 属性:

var filePathVO:DataVO = new FilePathVO(file.@title.toString(),
     file.location.toString());

真正的问题在于:

var files:XMLList = xml.files.file;

将其更改为

var files:XMLList = xml.file;

变量引用xml的xml根标签;无需使用 . 显式访问它xml.filesxml.files.file实际上会查找作为根 xml 标记的file直接子级的标记的直接子级的标记。files如果您的 xml 类似于:

<?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>
于 2010-07-01T04:41:09.657 回答