开发者

problem with reaching attributes in xml for use in object

开发者 https://www.devze.com 2023-02-23 15:04 出处:网络
I\'m trying to put each attribute from my xml file and store each attribute in an Object, but can\'t reach them. What am I doing wrong? Have 2 other classes which load images too, but not important fo

I'm trying to put each attribute from my xml file and store each attribute in an Object, but can't reach them. What am I doing wrong? Have 2 other classes which load images too, but not important for my question.

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=&quo开发者_JAVA百科t;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);
}


your very close! Here is a updated version of your loop code:

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);

    ...
}

I hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消