1

This is my XML data

public var treeData:XML =  <item name="vCenter Server" type="Server">
   <item name = "Datacenter" type="Datacenter" checked="false" moid="datacenter-85">
        <item name = "Cluster1" type="ClusterComputeResource" checked="false"  value="one" moid="cluster-85">
            <item  name = "Host1" ctype="Cluster1" type="HostSystem" checked="false" value="one" moid="hostystem-85"/>
            <item  name = "Host2" ctype="Cluster1" type="HostSystem" checked="false" value="one" moid="hostystem-86"/>
        </item>
         <item name = "Cluster2" type="ClusterComputeResource" checked="false" value="two" moid="cluster-86">
            <item name = "Host3" ctype="Cluster2" type="HostSystem" checked="false" value="two" moid="hostystem-87"/>
            <item name = "Host4" ctype="Cluster2" type="HostSystem" checked="false" value="two" moid="hostystem-88"/>
        </item>
        <item name = "Host 3" type="HostSystem" checked="true" moid="hostystem-89"> </item>
        <item name = "Host 4" type="HostSystem" checked="false" moid="hostystem-90"> </item>
   </item>  
</item>;

How can I iterate over the nested xml data in actionscript and show alerts:

if checked value is true, show You have selected

if checked value is false, show you have not selected in any item.

I have done following code but it is not working.

override public function isValid():Boolean
{
    if(treeData == null){
        Alert.show("Empty tree")
        return false;
    }
    else {
        for each(var item:XML in treeData)
        {

            for each(var item:XML in server.item.item)
            {
                if(item.@checked != true)
                {
                    Alert.show("You have not selected any item");
                    return false;
                }
                else if(item.@checked == true){
                    Alert.show("You have selected item");
                    return true;
                }
            }
        }
        return true;    
    }

}

I am iterating this xml on checkbox functionality

3
  • Are you trying to verify that all items are checked or just one that should be checked ? Could you explain more your problem ? Commented Sep 14, 2015 at 15:14
  • @akmozo i am trying to verify just one that should be checked Commented Sep 14, 2015 at 15:16
  • @Rushikeshjogle I edited your question to improve the code formatting, and I rephrased your question in hopes of making it more clear. Commented Sep 14, 2015 at 15:55

1 Answer 1

2

You can simplify life using E4X ( ECMAScript for XML ) to get the number of the selected items from your xml content like this :

trace(
    xml.*.(@checked == 'true').length() +       // level : 1 ( Datacenter ), gives : 0
    xml.*.*.(@checked == 'true').length() +     // level : 2 ( Cluster1, Cluster2, Host 3, Host 4 ), gives : 1
    xml.*.*.*.(@checked == 'true').length()     // level : 3 ( Host1, Host2, Host3, Host4 ), gives : 0
);      
// gives : 1

For more details about working with XML in AS3, take a look here.

Hope that can help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :) It Worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.