5 Replies Latest reply on Jul 16, 2008 1:31 PM by ntsiii

    Write an XML from Flex tree values

    Indianguy
      Hi,
      Greetings!!!

      I jus need to get the values of dynamically created tree component as an XML file or values to be populated in text area as XML format.

        • 1. Re: Write an XML from Flex tree values
          ntsiii Level 3
          That should work:
          text="{dataX.toXMLString()}"

          What is not working?

          Tracy
          • 2. Re: Write an XML from Flex tree values
            Indianguy Level 1
            Thanks for your reply....

            The problem is when we added an node dynamically, the XML is not getting updated.For e.g if i add a child node by clicking "Empty Child button" the XML is adding this child as a separate node,its not appending this Child node to the respective Parent.

            Can you provide me a solution to generate an XML for the complete set in tree by clicking a button.
            Advance Thanks!!!!
            • 3. Re: Write an XML from Flex tree values
              ntsiii Level 3
              Then displaying the xml is not your problem, you must add the child to the xml through the dataX variable.

              Why are you going through the tree.dataDescriptor to add a child node? You should just add it to the "dataX" XML node directly. I think your problem is you have lost the reference to the tree's dataProvider.

              I see you are re-declaring dataX inside the addEmptyChild function. that is almost certainly bad, at least it is bad naming.

              Tracy

              • 4. Re: Write an XML from Flex tree values
                Indianguy Level 1
                Currently When i select a node and Click on Generate XML button .. the textarea displays the selected node as XML in the Textarea. But my requirement is to get the complete tree structure as XML on Click on "Generate XML button" Can u pls provide me some sample on the same!!!!!

                here goes the code!!!

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]"
                backgroundGradientColors="[#333333, #0A0A0A]" color="#FFFFFF" creationComplete="init(), showAlert('Red');">
                <mx:Script>
                <![CDATA[

                import mx.controls.Alert;
                import mx.styles.StyleManager;
                import mx.effects.easing.Bounce;
                import mx.collections.ICollectionView;
                import mx.events.ListEvent;

                private var alert:Alert;
                private var alertCSS:CSSStyleDeclaration;

                private function init():void {
                alertCSS = StyleManager.getStyleDeclaration("Alert");
                }

                private function showAlert(color:Object):void
                {
                alertCSS.setStyle("modalTransparencyColor", color);
                alertCSS.setStyle("themeColor", color);
                }

                private function tree_itemClick(evt:ListEvent):void {
                var item:Object = Tree(evt.currentTarget).selectedItem;
                if (xmlBound2Tree.dataDescriptor.isBranch(item)) {
                xmlBound2Tree.expandItem(item, !xmlBound2Tree.isItemOpen(item), true);
                }
                }

                [Bindable]
                private var dataX:XML =<item label="Parent"></item>;

                private function addEmptyBranchDP():void
                {
                var dataX:XML = <item label={Eparent.text} isBranch="true"></item>;
                xmlBound2Tree.dataProvider.addItemAt(dataX,0);
                }

                private function addEmptyBranchDD():void
                {
                var dataX:XML = XML(xmlBound2Tree.selectedItem);
                if( dataX == null ) return;
                var select:int = xmlBound2Tree.selectedIndex;
                if (select == -1)
                {
                Alert.show("Please select the Node" , 'Message');
                }
                else
                {
                var newNode:XML = <item label={Eparent.text} isBranch="true"></item>;
                xmlBound2Tree.dataDescriptor.addChildAt(dataX, newNode, select,dataX);
                xmlBound2Tree.expandItem(xmlBound2Tree.selectedItem,true);

                }
                }

                private function addEmptyChild():void
                {
                var dataX:XML = XML(xmlBound2Tree.selectedItem);
                if( dataX == null ) return;
                var select:int = xmlBound2Tree.selectedIndex;
                if (select == -1)
                {
                Alert.show("Please select the Node", 'Message');
                }
                else
                {
                var newNode:XML = <item label={Eparent.text} isBranch="false"></item>;
                xmlBound2Tree.dataDescriptor.addChildAt(dataX, newNode, select,dataX);
                xmlBound2Tree.expandChildrenOf(xmlBound2Tree.selectedItem, true);

                }
                }

                private function removeEmployee():void
                {
                var node:XML = XML(xmlBound2Tree.selectedItem);
                if( node == null )
                return ;
                if(node.parent()== null)
                {
                var select:int= xmlBound2Tree.selectedIndex;
                if (select == -1)
                return;
                else
                xmlBound2Tree.dataProvider.removeItemAt(select);
                }
                else
                {
                var children:XMLList = XMLList(node.parent()).children();
                for(var i:Number=0; i < children.length(); i++)
                {
                if( children .@label == node.@label ) {
                delete children
                ;
                }
                }
                }
                }
                private function gen_xml():void
                {
                var node:XML = XML(xmlBound2Tree.selectedItem);
                textarea.text =node.toXMLString();
                }
                ]]>
                </mx:Script>

                <mx:HDividedBox left="10" right="10" top="25" bottom="10">
                <mx:VDividedBox height="100%" width="65%" >
                <mx:Panel width="100%" height="450" layout="absolute" title="Item Generator" >
                <mx:Tree id="xmlBound2Tree" dataProvider="{dataX}" labelField="@label" showRoot="true" editable="{RN.selected}" top="10" left="10" right="10"
                bottom="10" color="#141414" openEasingFunction="{mx.effects.easing.Bounce.easeInOut}"
                openDuration="500" useRollOver="{URO.selected}" dragEnabled="true" dragMoveEnabled="true"
                dropEnabled="true" itemClick="tree_itemClick(event);" />
                <mx:CheckBox label="| Use Roll Over" textAlign="left" color="#848484" id="URO" selected="false" right="15" bottom="10" labelPlacement="left" themeColor="#7B7B7B"/>
                <mx:CheckBox label="Rename" textAlign="left" color="#848484" id="RN" selected="false" right="128" bottom="10" labelPlacement="left" themeColor="#7B7B7B"/>
                <mx:ControlBar>
                <mx:Button label="Remove Selected Item" click="removeEmployee()" color="#000000" themeColor="#FF0000"/>
                <mx:Button label="Generate XML" click="gen_xml()" color="#000000" themeColor="#80FF4D"/>
                </mx:ControlBar>
                </mx:Panel>

                <mx:Panel title="Passing XML data by reference" layout="absolute" width="100%" height="62" color="#000000">
                <mx:FormItem label="Enter Node Name:" x="10" y="16" width="484">
                <mx:TextInput id="Eparent" text="Node Name" width="359"/>
                </mx:FormItem>
                <mx:Button label="ADD AS PARENT" click="addEmptyBranchDP();" y="48" x="130" width="175" height="25"/>
                <mx:Button label="ADD AS SUB PARENT" click="addEmptyBranchDD(), showAlert('Red');" themeColor="haloBlue" y="78" x="130" width="175" height="25"/>
                <mx:Button label="ADD AS CHILD" click="addEmptyChild(), showAlert('Red');" y="48" x="319" color="#000000" width="175" height="25"/>
                <!-- Display the resulting XML -->
                </mx:Panel>
                </mx:VDividedBox>
                <mx:Panel width="35%" height="100%" layout="absolute" title="XML Code">
                <mx:TextArea width="100%" color="#08365D" fontFamily="Calibri" fontStyle="italic" fontSize="14" fontWeight="normal" height="100%" text="{dataX.toXMLString()}" id="textarea"/>
                </mx:Panel>
                </mx:HDividedBox>
                <mx:Label x="10" y="-1" text="MENU GENERATOR"/>

                </mx:Application>
                • 5. Re: Write an XML from Flex tree values
                  ntsiii Level 3
                  Please re-read my last post.
                  Tracy