1 Reply Latest reply on Dec 26, 2008 10:24 AM by ntsiii

    Convert XML to ArrayCollection

    Johnking08 Level 1
      I have XML data from remote server in this format:
      <users>
      <user>
      <fname>John</fname>
      <lname>Smith</lname>
      <age> 20</age>
      <sex>male</sex>
      </user>
      <user>
      <fname>Mike</fname>
      <lname>Brown</lname>
      <age> 21</age>
      <sex>male</sex>
      </user>
      <user>
      <fname>Mary</fname>
      <lname>Collin</lname>
      <age> 17</age>
      <sex>female</sex>
      </user>
      </users>

      I need to an ArrayCollection in this form:
      [["John Smith", "20, "M"],
      ["Mike Brown", 21, "M"]
      ["Mary Collin", 17, "F"]]

      Can any help?
        • 1. Re: Convert XML to ArrayCollection
          ntsiii Level 3
          First, why not use XMLListCollection?
          var xmlResult:XML = event.result as XML:
          var xlcUsers:XMLListCollection = new XMLListCollection(xmlResult.user);

          An ArrayCollection of Arrays has few benefits over XLC. (Arrays are not bindable, for instance)

          But if you insist, you can do:
          var xmlResult:XML = event.result as XML:
          var acUsers:ArrayCollection = new ArrayCollection()
          var xlUsers:XMLList = xmlResult.user;
          var xmlUser:XML;
          for (var i:int=0;i<xlUsers;i++) {
          xmlUser = xlUsers[ i ];
          acUsers.addItem([xmlUser.fname.text(),xmlUser.age.text(),xmlUser.sex.text() ])
          ...
          Tracy