2 Replies Latest reply on Aug 24, 2007 9:30 PM by chris.huston.t10

    Parsing Odd XML

    TorrenteDev Level 1
      In my current application, I'm recieving XML through an HTTPService. The xml is rather odd, however, and I'm not sure how I'd get it into a datagrid. Example:

      <ROW ID="1">
      <FIELD ALIAS="accountNumber" TYPE="CHARACTER" VALUE="111222"/>
      <FIELD ALIAS="accountId" TYPE="CHARACTER" VALUE="12345"/>
      <FIELD ALIAS="application.applicationCode" TYPE="CHARACTER" VALUE="DDA"/>
      <FIELD ALIAS="accountCurBalance" TYPE="CHARACTER" VALUE="4000"/>
      <FIELD ALIAS="accountStatus" TYPE="CHARACTER" VALUE="O"/>
      <FIELD ALIAS="accountRelationship" TYPE="CHARACTER" VALUE="Primary"/>
      <FIELD ALIAS="accountMaturity" TYPE="DATE" VALUE="2003-05-18 11:15:20.535"/>
      <FIELD ALIAS="institution.institutionCode" TYPE="CHARACTER" VALUE="8888"/>
      <FIELD ALIAS="product.productCode" TYPE="CHARACTER" VALUE="400"/>
      <FIELD ALIAS="relationshipManager" TYPE="CHARACTER" VALUE="Dean"/>
      </ROW>
      <ROW ID="2" >
      <FIELD ALIAS="accountNumber" TYPE="CHARACTER" VALUE="111222"/>
      <FIELD ALIAS="accountId" TYPE="CHARACTER" VALUE="76547"/>
      <FIELD ALIAS="application.applicationCode" TYPE="CHARACTER" VALUE="DDA"/>
      <FIELD ALIAS="accountCurBalance" TYPE="CHARACTER" VALUE="9000"/>
      <FIELD ALIAS="accountStatus" TYPE="CHARACTER" VALUE="C"/>
      <FIELD ALIAS="accountRelationship" TYPE="CHARACTER" VALUE="Primary"/>
      <FIELD ALIAS="accountMaturity" TYPE="DATE" VALUE="2003-05-18 11:15:20.535"/>
      <FIELD ALIAS="institution.institutionCode" TYPE="CHARACTER" VALUE="8988"/>
      <FIELD ALIAS="product.productCode" TYPE="CHARACTER" VALUE="200"/>
      <FIELD ALIAS="relationshipManager" TYPE="CHARACTER" VALUE="Dean1"/>
      </ROW>

      The column names are in the ALIAS attribute, and the values that should go under those columns are in the VALUES attribute. Any ideas how I'd get this into a datagrid?

      All help is appreciated.
        • 1. Re: Parsing Odd XML
          JKohn99 Level 1
          I would create a proxy object with the appropriate variables. Then parse the xml creating new objects, put them in an ArrayCollection and hand it to the grid.

          If the xml is consistent you could change your result format to object and let flex
          to the work for you. However somtimes flex will not make right decisions.
          • 2. Re: Parsing Odd XML
            chris.huston.t10 Level 3
            Here is one option for parsing the XML into an ArrayCollection:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
            <mx:Script>
            <![CDATA[
            import mx.collections.ArrayCollection;
            private var accountXML:XML =
            <root>
            <ROW ID="1">
            <FIELD ALIAS="accountNumber" TYPE="CHARACTER" VALUE="111222"/>
            <FIELD ALIAS="accountId" TYPE="CHARACTER" VALUE="12345"/>
            <FIELD ALIAS="application.applicationCode" TYPE="CHARACTER" VALUE="DDA"/>
            <FIELD ALIAS="accountCurBalance" TYPE="CHARACTER" VALUE="4000"/>
            <FIELD ALIAS="accountStatus" TYPE="CHARACTER" VALUE="O"/>
            <FIELD ALIAS="accountRelationship" TYPE="CHARACTER" VALUE="Primary"/>
            <FIELD ALIAS="accountMaturity" TYPE="DATE" VALUE="2003-05-18 11:15:20.535"/>
            <FIELD ALIAS="institution.institutionCode" TYPE="CHARACTER" VALUE="8888"/>
            <FIELD ALIAS="product.productCode" TYPE="CHARACTER" VALUE="400"/>
            <FIELD ALIAS="relationshipManager" TYPE="CHARACTER" VALUE="Dean"/>
            </ROW>
            <ROW ID="2" >
            <FIELD ALIAS="accountNumber" TYPE="CHARACTER" VALUE="111223"/>
            <FIELD ALIAS="accountId" TYPE="CHARACTER" VALUE="76547"/>
            <FIELD ALIAS="application.applicationCode" TYPE="CHARACTER" VALUE="DDA"/>
            <FIELD ALIAS="accountCurBalance" TYPE="CHARACTER" VALUE="9000"/>
            <FIELD ALIAS="accountStatus" TYPE="CHARACTER" VALUE="C"/>
            <FIELD ALIAS="accountRelationship" TYPE="CHARACTER" VALUE="Primary"/>
            <FIELD ALIAS="accountMaturity" TYPE="DATE" VALUE="2003-05-18 11:15:20.535"/>
            <FIELD ALIAS="institution.institutionCode" TYPE="CHARACTER" VALUE="8988"/>
            <FIELD ALIAS="product.productCode" TYPE="CHARACTER" VALUE="200"/>
            <FIELD ALIAS="relationshipManager" TYPE="CHARACTER" VALUE="Dean1"/>
            </ROW>
            </root>;

            [Bindable] private var accountAC:ArrayCollection = new ArrayCollection();

            private function convertXML():void {
            var accountObj:Object = new Object();
            for each (var rows:XML in accountXML.ROW) {
            for (var colData:String in rows.FIELD) {
            accountObj[rows.FIELD.@ALIAS[colData]] = rows.FIELD.@VALUE[colData];
            }
            accountAC.addItem(accountObj);
            accountObj = new Object();
            }
            }

            ]]>
            </mx:Script>
            <mx:Button x="10" y="10" label="convert xml" click="convertXML()"/>
            <mx:DataGrid x="10" y="39" dataProvider="{accountAC}">
            <mx:columns>
            <mx:DataGridColumn headerText="Accound Number" dataField="accountNumber"/>
            <mx:DataGridColumn headerText="Account ID" dataField="accountId"/>
            <mx:DataGridColumn headerText="Ap Code" dataField="application.applicationCode"/>
            <mx:DataGridColumn headerText="Cur Bal" dataField="accountCurBalance"/>
            <mx:DataGridColumn headerText="Acnt Status" dataField="accountStatus"/>
            <mx:DataGridColumn headerText="Relationships" dataField="accountRelationship"/>
            <mx:DataGridColumn headerText="Maturity" dataField="accountMaturity"/>
            <mx:DataGridColumn headerText="Inst Code" dataField="institution.institutionCode"/>
            <mx:DataGridColumn headerText="Prod Code" dataField="product.productCode"/>
            <mx:DataGridColumn headerText="Rel Mngr" dataField="relationshipManager"/>
            </mx:columns>
            </mx:DataGrid>
            </mx:Application>

            I hope this helps.

            Vygo