2010-07-01 33 views
3

我有一個問題,加載和訪問我的新項目中的值對象的數據..我通過一個服務,其中包含資產文件的標題和位置加載一個XML文件,我需要能夠通過指定標題和值對象retrieiving它訪問的資源文件的位置..我使用的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([email protected](), 
    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']); 

它返回undefined ..任何想法?

感謝馬丁:-)

回答

1

你忘了@的位置屬性在這條線:

var filePathVO:DataVO = new FilePathVO([email protected](), 
    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>