10 Replies Latest reply on Jun 28, 2006 7:05 AM by inlineblue

    convert xml and put in array

    FrankAtcomm
      Hello,

      I want to load all the labels of my buttons etc. from a database so I can make my application multi-language. For this the xml loaded from the database should be put in an array. Who can give me a little hand with this ?

      This is the code I have been experimenting with:

      public function FillArrayLabels():void {
      var x:XML = new XML(WsLabels.WsGetLabels.lastResult);

      //var arrLabels:Array = mx.utils.ArrayUtil.toArray(WsLabels.WsGetLabels.lastResult);

      showErrorDialog(x.children());
      //showErrorDialog(x.children().length());

      //for (var i:int = 0; i <= x.children().length(); i++) {
      //showErrorDialog(x.children );
      //}
      }

      The result of "showErrorDialog(x.children());" is:

      <result>
      <id>1</id>
      <showLabel>Home</showLabel>
      </result>
      <result>
      <id>2</id>
      <showLabel>Clients</showLabel>
      </result>
      <result>
      <id>3</id>
      <showLabel>Orders</showLabel>
      </result>

      And this should be converted into:
      arrLabels[1] = "Home";
      arrLabels[2] = "Clients";
      arrLabels[3] = "Orders";

      The loop I was trying to build, only gives me errors ...

      Best regards,
      Frank
        • 1. Re: convert xml and put in array
          ntsiii Level 3
          What errors? Except for the line "//showErrorDialog(x.children);" the loop looks ok. You are definitely on the right track.

          Tracy
          • 2. Re: convert xml and put in array
            ntsiii Level 3
            Post a standalone sample app and I will give you more detail.
            • 3. Re: convert xml and put in array
              FrankAtcomm Level 1
              Hello ntsiii,

              Knowing I was on the right track, searched a little further and got it up and running with this function:

              public function FillArrayLabels():void {
              var x:XML = new XML(WsLabels.WsGetLabels.lastResult);

              //showErrorDialog(x.children());
              //showErrorDialog(x.children().length());

              var arrLabels:Array = new Array();

              for (var i:int = 0; i < x.children().length(); i++) {
              var doId:int = x.children() .children()[x.children().id.childIndex()].valueOf();
              var doText:String = x.children() .children()[x.children().showLabels.childIndex()].valueOf();

              arrLabels[doId] = doText;

              //showErrorDialog(i + ": " + doId + " " + doText+ " " + arrLabels[doId]);
              }
              }

              Thanks for the help !
              • 4. convert xml and put in array
                FrankAtcomm Level 1
                One additional question ...

                I now have my array filled. For instance arrLabels[5] = "Reports"

                But this doesn't seem to work:
                <mx:Canvas label="{arrLabels[5]}" width="100%" height="100%" enabled="false" id="canvasReports">

                Any ideas ?
                I defined the array as a public var ...

                public var arrLabels:Array = new Array();
                • 5. Re: convert xml and put in array
                  ntsiii Level 3
                  Well, without looking into the docs, Canvas is a container and almost certainly does not have a label property. Hmm, unless it is used as a child of an Accordion...

                  Anyway, try the same binding with a mx:Label tag inside the canvas.

                  Tracy
                  • 6. Re: convert xml and put in array
                    FrankAtcomm Level 1
                    I'm using the canvas with tabs ...

                    But anyway, label isn't working too.

                    <mx:Canvas label="{arrLabels[5]}" width="100%" height="100%" enabled="false" id="canvasReports">
                    <mx:Label x="684" y="10" text="{arrLabels[5]}" width="150" id="lblTest"/>

                    Both don't show anything

                    Code:

                    [Bindable]
                    public var arrLabels:Array = new Array();

                    public function FillArrayLabels():void {
                    var x:XML = new XML(WsLabels.WsGetLabels.lastResult);

                    for (var i:int = 0; i < x.children().length(); i++) {
                    var doId:int = x.children() .children()[x.children().id.childIndex()].valueOf();
                    var doText:String = x.children() .children()[x.children().showLabels.childIndex()].valueOf();

                    arrLabels[doId] = doText;
                    }

                    showErrorDialog("check: " + arrLabels[5]);
                    //lblTest.text = arrLabels[5];
                    }


                    • 7. Re: convert xml and put in array
                      FrankAtcomm Level 1
                      I'm using the canvas with tabs ...

                      But anyway, label isn't working too.

                      <mx:Canvas label="{arrLabels[5]}" width="100%" height="100%" enabled="false" id="canvasReports">
                      <mx:Label x="684" y="10" text="{arrLabels[5]}" width="150" id="lblTest"/>

                      Both don't show anything

                      Code:

                      [Bindable]
                      public var arrLabels:Array = new Array();

                      public function FillArrayLabels():void {
                      var x:XML = new XML(WsLabels.WsGetLabels.lastResult);

                      for (var i:int = 0; i < x.children().length(); i++) {
                      var doId:int = x.children() .children()[x.children().id.childIndex()].valueOf();
                      var doText:String = x.children() .children()[x.children().showLabels.childIndex()].valueOf();

                      arrLabels[doId] = doText;
                      }

                      showErrorDialog("check: " + arrLabels[5]);
                      //lblTest.text = arrLabels[5];
                      }


                      • 8. Re: convert xml and put in array
                        inlineblue Level 1
                        You cannot bind to individual elements of an array because an array doesn't fire events when individual elements are changed. The binding fires only when the entire array is set. Your bindings should fire if you did this instead:

                        var tempArr: Array = new Array();
                        for (...)
                        {
                        //buid up tempArr
                        }
                        arrLabels = tempArr; //Should trigger the binding
                        • 9. Re: convert xml and put in array
                          FrankAtcomm Level 1
                          thanx ! indeed that works

                          for all those text="{arrLabels[5]}" in the code I get the following warning:

                          Data binding will not be able to detect changes when using square bracket operator. For Array, please use ArrayCollection.getItemAt() instead.

                          Has this something to do with the problem I was encountering ?
                          • 10. Re: convert xml and put in array
                            inlineblue Level 1
                            Yes, that warning messge is exactly the problem you were encountering. The suggestion in the message is to use an ArrayCollection instead of a basic array since the ArrayCollection is able to trigger change events when individual elements are updated. However, I don't believe you need to go that far in your case.