Flash / Flex / ActionScript/XML/attributes — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:14, 26 мая 2010

Access an element"s attributes using E4X attributes wildcard (@*)

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.@*);
    }
  }
}



Accesses first attribute by using E4X"s attributes wildcard syntax

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.@*[0]);
    }
  }
}



Accessing Attributes and Elements Whose Names Contain Reserved Characters: attribute( ), child( ), or [] operator.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var saleEndsDate:XML = <DATE TIME-ZONE="PST">1, 2006</DATE>
        //trace(saleEndsDate.@TIME-ZONE);              
        trace(saleEndsDate.attribute("TIME-ZONE"));  
        trace(saleEndsDate.@["TIME-ZONE"]);          
    }
  }
}



Adding New Attributes and Elements

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK/>;
        
        novel.@ISBN = "0000000000";
        novel.TITLE = "ActionScript";
        novel.AUTHOR = "J, J";
        novel.PUBLISHER = "Books Ltd";
        novel.SETTING.CITY = "Dublin";
        novel.SETTING.COUNTRY = "Ireland";
        
        trace(novel);
    }
  }
}



Assign the desired attributes the desired values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK/>;
        
        novel.@ISBN = "0000000000";
        novel.TITLE = "ActionScript";
        novel.AUTHOR = "J, J";
        novel.PUBLISHER = "Books Ltd";
        novel.SETTING.CITY = "Dublin";
        novel.SETTING.COUNTRY = "Ireland";
        
        novel.SETTING.@CITY = "Dublin";
        novel.SETTING.@COUNTRY = "Ireland";
        
        trace(novel);
        
    }
  }
}



Attributes in an XMLList can be accessed using the array-access operator ([]).

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.attributes()[0]);
    }
  }
}



Changing an Attribute Value

 
To change an XML attribute value, simply assign the attribute any new value in an assignment statement. 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
          
        novel.@ISBN = "0684800713";
        
        trace(novel);
    }
  }
}



Returns an XMLList that contains one XML instance

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.attribute("ISBN"));
    }
  }
}



Returns an XMLList that contains one XML instance, representing the attribute, with variable-access syntax.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.@ISBN);
    }
  }
}



To access an element"s attributes: attributes()

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.attributes());
    }
  }
}



To access the XML instance contained by the XMLList returned by novel.@ISBN

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        trace(novel.@ISBN[0]);
    }
  }
}



To loop over all of the attributes of an element, use a for each loop.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var fruit:XML = <fruit name="Apple" color="red" />;
        
        for each ( var attribute:XML in fruit.attributes(  ) ) {
          trace( attribute.name(  ) + " = " + attribute.toString(  ) );
        }
    }
  }
}



Unusual behavior can be used to collect a group of attributes into a single attribute

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var books:XML = <BOOKS>
            <BOOK ISBN="0000000000"/>
            <BOOK ISBN="1111111111"/>
            <BOOK ISBN="2222222222"/>
          </BOOKS>;
        
        var order:XML = <ORDER ITEMS=""/>;
        order.@ITEMS = books.*.@ISBN;
        
        trace(order);
    }
  }
}



Use the name( ) method on the attribute reference to retrieve its name

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var fruit:XML = <fruit name="Apple" color="red" />;
        
        trace( fruit.attributes(  )[1].name(  ) );

    }
  }
}