在xml中到达属性的问题

时间:2011-04-12 02:39:52

标签: xml actionscript-3

我正在尝试从我的xml文件中放入每个属性并将每个属性存储在一个Object中,但是无法访问它们,我做错了什么?还有2个其他类加载图像,但对我的问题并不重要。感谢任何答案

XML:

<personal>
<person id="1" name="Oprah Winfrey" image="oprah-winfrey.jpg" title="administrator"></person>
<person id="2" name="Zlatan Ibrahimovic" image="zlatan-ibrahimovic.jpg" title="technician"></person>
<person id="3" name="Barack Obama" image="barack-obama.jpg" title="CEO"></person>
</personal>

AS3:

    private var _items:Array = new Array();
    private var _xmlLoader:URLLoader = new URLLoader();
    private var _Loader:Loader;
    private var _urlRequest:URLRequest;
    private var _xml:XML;

    public function Main(){
        _xmlLoader.load(new URLRequest("personal.xml"));
        _xmlLoader.addEventListener(Event.COMPLETE, onXmlLoadComplete);

    }

    private function onXmlLoadComplete(e:Event):void{

        _xml = new XML(e.target.data);
        var _xmlList:XMLList = _xml.person;
            for each(var node in _xmlList){
                for each(var attribute in node.attributes())
                //trace(attribute.name()+"::"+attribute) //will output each attribute
                //trace("********Node End*********")
                var obj:Object = attribute;
                trace("obj "+obj.('image')); //outputs "title" node from xml file
                var item:ImageItem = new ImageItem(obj.image, 
                                                obj.name, 
                                                obj.title);
            addChild(item);

            _items.push(item);  
            }

        trace("items "+_items.length);


    }

1 个答案:

答案 0 :(得分:0)

你非常接近!以下是循环代码的更新版本:

xml = new XML(e.target.data);

for each (var node:XML in xml.person) {
    var obj:Object = {};
    obj.name = node.@name;
    obj.image = node.@image;
    obj.title = node.@title;

    var item:ImageItem = new ImageItem(obj.image, obj.name, obj.title);

    ...
}

我希望有所帮助!