4 Replies Latest reply on Jul 27, 2006 4:34 AM by peterent

    How do I convert XML to Object?

    jpwrunyan Level 1
      The short version:
      How can I convert
      private var myXml:XML =
      <root>
      <item id="1" data="1" />
      <item id="2" data="2" />
      </root>

      into:
      private var root:Object = {
      item: [{id:"1", data:"1"}, {id: "2", data:"2"}]
      }
      (or some such similar format)???

      Ok, I am currently just trying to make my application work with dummy XML data generated internally. I am NOT using Model and external XML. Eventually, my internal dummy data will be replaced with XML results from a server. However, even these results will go through some precessing before eventually arriving at their control destination so I want to guarantee my controls can convert XML data into Object or Array format.

      Why is it that Model seems to be able to convert external XML into Objects and Arrays and such, but there is no way to do this internally with AS? I mean, there isn't even a utility or anything to do this. So far I can only think of converting the xml into a string and writing my own unweildy function to convert it to Object. There doesn't even seem to be a way to find out what the attribute names are in the XML instance, only their values. What gives?

      any advice would be greatly appreciated.
        • 1. Re: How do I convert XML to Object?
          peterent Level 2
          Look into E4X - it is much more easy to use. For example, using your XML:

          myXML.item is automatically an array (actually it is an XMLList) and myXML.item[0].@id is 1 and myXML.item[1].@data is 2. You can also iterate over the array:

          for(var k:int=0; k < myXML.item.length; k++) { trace(myXML.item[k].@id+" = "+myXML.item[k].@data); }

          The @ notation is used to address attributes (id and data in your case). Dot notation addresses children. For example, if you had:

          <item id="1"><data>1</data></item> <item id="2"><data>2</data>

          then you would do: myXML.item[0].data to get 1.

          The other benefit is that you can use XMLListCollection as a dataProvider to DataGrid, TileList, etc.
          • 2. Re: How do I convert XML to Object?
            ntsiii Level 3
            Yeah, man e4x takes a bit of getting used to but it rocks.

            Tracy
            • 3. Re: How do I convert XML to Object?
              jpwrunyan Level 1
              I don't understand... I thought XML was E4X. I thought the legacy XML was XMLDocument.
              So if I am mistaken on this then my question simply becomes: how do I turn XML into E4X the way Model does?

              By the way, I can turn the XML in my first post into an XMLList by doing:
              myXml.children() and even assign to a DataGrid using myGrid.dataProvider = myXml.children() but the data remains in XML format which is not what I want. The reason being, it doesn't let me access properties (eg myGrid.selectedItem.label etc.)

              Does this make sense? Are you following?
              It's easy to display XMLList in a control, but it is not easy to get that data back out of its control. Ergo, it seems a good idea to convert it to something else (Object or Array) beforehand.
              • 4. Re: How do I convert XML to Object?
                peterent Level 2
                You don't need to convert to an ArrayCollection or objects to use your XML in a DataGrid. Unless you really, really do not want XML.

                Using the sample XML in the post above, you can display it in a DataGrid like this:

                <mx:DataGrid dataProvider="{myXML.item}">
                <mx:columns>
                <mx:DataGridColumn dataField="@id" headerText="ID" />
                <mx:DataGridColumn dataField="@data" headerText="Data" />
                </mx:columns>
                </mx:DataGrid>

                Note the use of @ in the dataField. And you can do: mxGrid.selectedItem.@id since the selectedItem is an XML element.

                However, if you really want to convert this to an ArrayCollection of objects, you will have to walk through the XML structure and do it by hand. We did ship a class with Flex 1.5 that converted XML into objects so if you have Flex 1.5 you can look into that. It was XMLObjectOutput in the extras folder of the Flex 1.5 install.