5 Replies Latest reply on Jul 31, 2009 9:22 AM by Gregory Lafrance

    [Flex] Create XML from component id of mxml

    Nikynik

      Hi,

      my english it's no very good!!!!

       

      it's possible to create a Xml automatically by the id of some Text Input of my mxml page?

       

      examble of XML:

       

      <document>

           <id>id_TextInput</id>

           <value>id_TextInput.text</value>

       

      </document>

        • 1. Re: [Flex] Create XML from component id of mxml
          Nikynik Level 1

          I have some textinput and i have the necessity to create this xml whith ALL TEXTINPUT

          • 2. Re: [Flex] Create XML from component id of mxml
            Gregory Lafrance Level 6

            The following code answers your question.

             

            If this post answers your question or helps, please mark it as such.

             

            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
              creationComplete="init();">
              <mx:Script>
                <![CDATA[
                  private var myXML:XML = <root/>;

                  private function init():void{
                    for each(var obj:Object in this.getChildren()){
                      if(obj is TextInput){
                        var ti:TextInput = obj as TextInput;
                        var xml:XML = <document>
                          <id>{ti.id}</id>
                          <value>{ti.text}</value>
                        </document>;
                        myXML.appendChild(xml);
                      }
                    }
                    for each(var xmlObj:XML in myXML..document){
                      trace(xmlObj.toXMLString() + "\n**********");
                    }
                  }
                ]]>
              </mx:Script>
              <mx:Button label="Btn"/>
              <mx:TextInput id="txt1" text="Text1"/>
              <mx:Button label="Btn"/>
              <mx:TextInput id="txt2" text="Text2"/>
              <mx:Button label="Btn"/>
              <mx:TextInput id="txt3" text="Text3"/> 
            </mx:Application>

            • 4. Re: [Flex] Create XML from component id of mxml
              Nikynik Level 1

              i have an other problem....

               

              my mxml is:

               

              <mx:Vbox>

                <mx:Panel>

                   <mx:combobox/>

                   <mx:TextInput/>

              ..

              ...

                 </mx:Panel>

              ...

              ...

              ...<mx:Panell>

              ...

              ...

              ...

                     <mx:TextInput/>

                   ...

                   ...

                   ...

                 </mx:Panel>

              </mx:Panel>

               

               

              and so in this method I can't find TextInput...

              • 5. Re: [Flex] Create XML from component id of mxml
                Gregory Lafrance Level 6

                Here you go, please mark this as the answer if it answers your question :

                 

                 

                <?xml version="1.0"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  creationComplete="init();">
                  <mx:Script>
                    <![CDATA[
                      import mx.core.Container;
                      private var myXML:XML = <root/>;

                      private function init():void{
                        getTextInput(this);
                        for each(var xmlObj:XML in myXML..document){
                          trace(xmlObj.toXMLString() + "\n**********");
                        }
                      }
                     
                      private function getTextInput(obj:Object):void{
                        if(obj is Container){
                          for each(var child:Object in obj.getChildren()){
                            getTextInput(child);
                          }      
                        }else{
                          checkObject(obj);
                        }
                      }
                     
                      private function checkObject(object:Object):void{
                        if(object is TextInput){
                          var ti:TextInput = object as TextInput;
                          var xml:XML = <document>
                            <id>{ti.id}</id>
                            <value>{ti.text}</value>
                          </document>;
                          myXML.appendChild(xml);
                        }
                      }
                    ]]>
                  </mx:Script>
                  <mx:Button label="Btn"/>
                  <mx:TextInput id="txt1" text="Text1"/>
                  <mx:Button label="Btn"/>
                  <mx:TextInput id="txt2" text="Text2"/>
                  <mx:Button label="Btn"/>
                  <mx:TextInput id="txt3" text="Text3"/> 
                  <mx:Panel>
                    <mx:Button label="PanelBtn"/>
                    <mx:TextInput id="pnltxt1" text="PanelText1"/>
                    <mx:Button label="PanelBtn"/>
                    <mx:TextInput id="pnltxt2" text="PanelText2"/>
                    <mx:Button label="PanelBtn"/>
                    <mx:TextInput id="pnltxt3" text="PanelText3"/>    
                  </mx:Panel>
                </mx:Application>