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

它返回不确定的..有什么想法吗?

有帮助吗?

解决方案

你忘了 @ 对于此行上的位置属性:

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.files. xml.files.file 实际寻找 file 是直接子的标签 files 标签是root-xml标签的直接子。如果您的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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top