A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Check if XML attribute is set?

  1. #1
    Member
    Join Date
    Jan 2009
    Posts
    90

    Question Check if XML attribute is set?

    I have a piece of code where I want to set a variable from an XML attribute only if that attribute is defined, otherwise do nothing.

    Compare the following xml snippets:
    <component name="gun" size="0">
    vs.
    <component name="armor">

    In the example above, the "gun" has set the size and the "armor" has not.

    How can I check whether an XML item (such as the two components above) has set a particular attribute (such as size)?

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    the long way to do it is as follows:
    PHP Code:
    var xml:XML = <root>
    <
    component name="gun" size="0"></component>
    <
    component name="gun"></component>
    </
    root>;

    var 
    count:uint=xml.component.length();
    while (
    count--){
        var 
    size:String xml.component[count].@size.toString();
        if(
    size.length 0){
            
    trace("component " count " has size set.");
        };

    however, you can grab all the components that have a size that meets a certain comparison really easily in an xml list, then perform actions on that list:
    PHP Code:
    var xml:XML = <root>
    <
    component name="gun" size="0"></component>
    <
    component name="gun" size="2"></component>
    <
    component name="gun" size="5"></component>
    <
    component name="gun" size="10"></component>
    <
    component name="gun"></component>
    <
    component name="gun"></component>
    <
    component name="gun"></component>
    </
    root>;

    var 
    hasguns:XMLList xml.component.(attribute("size") > 1);
    var 
    count:uint=hasguns.length();
    while (
    count--){
        
    trace(hasguns[count].@size);


  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    26
    PHP Code:
    var list:XMLList =
    XMLList(<component name="gun" size="0"/> +
    <
    component name="armor"/>);
    list.@*.(
    name() == "size" setSize(parent()) : "");
    function 
    setSize(value:XML):void
    {
        
    trace("setting size of:"value.@name"to:"value.@size);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center