8 Replies Latest reply on Mar 23, 2010 12:00 PM by AdrianWiecek

    xml to datagrid problem

    goflashman

      Im new to flex and am having a problem getting my data into a datagrid. This should be simple but I have run out of ideas on making this work.

      I cannot control the data. it arrives in this form. can it be integrated into a datagrid? I need the text between the Field tags.

      Any ideas would be very helpful.

      Thanks in advance!!!

       

      My data:

      <?xml version='1.0' encoding='UTF-8' ?>

      <Report>

       

      <Data>

      <Row>

      <Field name = "EL_VALUES_FIELD8" pos= "1">NAM</Field>

      <Field name = "EL_VALUES_FIELD9" pos= "2">United States</Field>

      <Field name = "EL_VALUES_FIELD10" pos= "3">PUBLIC POWER DISTRICT</Field>

      <Field name = "EL_VALUES_FIELD11" pos= "4">FORT BUMPER</Field>

      <Field name = "EL_VALUES_FIELD12" pos= "5">BOO</Field>

      <Field name = "EL_VALUES_FIELD13" pos= "6">SINCLAIR</Field>

      <Field name = "EL_VALUES_FIELD14" pos= "7">NE</Field>

      <Field name = "EL_VALUES_FIELD5" pos= "8">03/29/2010 12:00:00 AM</Field>

      <Field name = "EL_VALUES_FIELD6" pos= "9">ELEMENT,PRO-RO-430-HR-WT</Field>

      <Field name = "EL_VALUES_FIELD8" pos= "10">3021590</Field>

      <Field name = "EL_VALUES_FIELD7" pos= "11">36</Field>

      <Field name = "S.EL_VALUES_FIELD10||'-'||S.EL" pos= "12">FORT CALHOUN</Field>

      <Field name = "EL_VALUES_FIELD15" pos= "13">N01937</Field>

      </Row>

      <Row>

      <Field name = "EL_VALUES_FIELD8" pos= "1">APAC</Field>

      <Field name = "EL_VALUES_FIELD9" pos= "2">India</Field>

      <Field name = "EL_VALUES_FIELD10" pos= "3">Petro</Field>

      <Field name = "EL_VALUES_FIELD11" pos= "4">Water Plant</Field>

      <Field name = "EL_VALUES_FIELD12" pos= "5">BOOT</Field>

      <Field name = "EL_VALUES_FIELD13" pos= "6"></Field>

      <Field name = "EL_VALUES_FIELD14" pos= "7">Johan Barru</Field>

      <Field name = "EL_VALUES_FIELD5" pos= "8">04/30/2010 12:00:00 AM</Field>

      <Field name = "EL_VALUES_FIELD6" pos= "9">ELEMENT,PRO-RO-400-HR-WT</Field>

      <Field name = "EL_VALUES_FIELD8" pos= "10">3006198</Field>

      <Field name = "EL_VALUES_FIELD7" pos= "11">84</Field>

      <Field name = "S.EL_VALUES_FIELD10||'-'||S.EL" pos= "12">Petchem-Water</Field>

      <Field name = "EL_VALUES_FIELD15" pos= "13">PROXY7</Field>

      </Data>

      </Row>

      <Report>

        • 1. Re: xml to datagrid problem
          Flex harUI Adobe Employee

          You will need to use labelFunction to extract what you want from the row

          • 2. Re: xml to datagrid problem
            naganagesh

               For Datagrid you can use code like :

             

            <mx:DataGridColumn  headerText="Name" dataField="@name"/>

            <mx:DataGridColumn  headerText="Position" dataField="@pos"/>

            • 3. Re: xml to datagrid problem
              goflashman Level 1

              Thanks for responding!

              If you look at the data, what im trying to extract exists outside of the "Field" tag.

              @name is an attribute of Field. Im interested in what exists between the opening and closing Field tags.

              • 4. Re: xml to datagrid problem
                AdrianWiecek Level 3

                Hi,

                 

                This is not a very elegant solution, but maybe it will help you:

                 

                 

                <mx:Script>

                    <![CDATA[

                   

                    public function myLabelFunction(item:XML, column:DataGridColumn):String

                    {

                         return item.Field[dg.columns.indexOf(column)];

                    }

                   

                    ]]>

                </mx:Script>

                 

                <mx:XML id="xml" format="e4x">

                    <Report>

                          <Data>

                               <Row>

                                    <Field name = "EL_VALUES_FIELD8" pos= "1">NAM</Field>

                                    <Field name = "EL_VALUES_FIELD9" pos= "2">United States</Field>

                                    <Field name = "EL_VALUES_FIELD10" pos= "3">PUBLIC POWER DISTRICT</Field>

                                    <Field name = "EL_VALUES_FIELD11" pos= "4">FORT BUMPER</Field>

                                    <Field name = "EL_VALUES_FIELD12" pos= "5">BOO</Field>

                                    <Field name = "EL_VALUES_FIELD13" pos= "6">SINCLAIR</Field>

                                    <Field name = "EL_VALUES_FIELD14" pos= "7">NE</Field>

                                    <Field name = "EL_VALUES_FIELD5" pos= "8">03/29/2010 12:00:00 AM</Field>

                                    <Field name = "EL_VALUES_FIELD6" pos= "9">ELEMENT,PRO-RO-430-HR-WT</Field>

                                    <Field name = "EL_VALUES_FIELD8" pos= "10">3021590</Field>

                                    <Field name = "EL_VALUES_FIELD7" pos= "11">36</Field>

                                    <Field name = "S.EL_VALUES_FIELD10||'-'||S.EL" pos= "12">FORT CALHOUN</Field>

                                    <Field name = "EL_VALUES_FIELD15" pos= "13">N01937</Field>

                               </Row>

                               <Row>

                                    <Field name = "EL_VALUES_FIELD8" pos= "1">APAC</Field>

                                    <Field name = "EL_VALUES_FIELD9" pos= "2">India</Field>

                                    <Field name = "EL_VALUES_FIELD10" pos= "3">Petro</Field>

                                    <Field name = "EL_VALUES_FIELD11" pos= "4">Water Plant</Field>

                                    <Field name = "EL_VALUES_FIELD12" pos= "5">BOOT</Field>

                                    <Field name = "EL_VALUES_FIELD13" pos= "6"></Field>

                                    <Field name = "EL_VALUES_FIELD14" pos= "7">Johan Barru</Field>

                                    <Field name = "EL_VALUES_FIELD5" pos= "8">04/30/2010 12:00:00 AM</Field>

                                    <Field name = "EL_VALUES_FIELD6" pos= "9">ELEMENT,PRO-RO-400-HR-WT</Field>

                                    <Field name = "EL_VALUES_FIELD8" pos= "10">3006198</Field>

                                    <Field name = "EL_VALUES_FIELD7" pos= "11">84</Field>

                                    <Field name = "S.EL_VALUES_FIELD10||'-'||S.EL" pos= "12">Petchem-Water</Field>

                                    <Field name = "EL_VALUES_FIELD15" pos= "13">PROXY7</Field>

                               </Row>

                          </Data>

                     </Report>

                </mx:XML>

                 

                 

                <mx:DataGrid id="dg" dataProvider="{xml.Data.Row}" labelFunction="myLabelFunction">

                     <mx:columns>

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                          <mx:DataGridColumn />

                     </mx:columns>

                </mx:DataGrid>

                 

                 

                 

                Clearly you have more complicated logic which requires to check column number etc...

                 

                Regards,

                Adrian

                 

                Message was edited by: AdrianWiecek

                • 5. Re: xml to datagrid problem
                  goflashman Level 1

                  Adrain,,

                   

                  thanx very much for your help. after weeks of wrangling your solution gets the job done! I greatly appreciate your help.

                  • 6. Re: xml to datagrid problem
                    AdrianWiecek Level 3

                    No problem, we all have the same issues:)

                     

                    I think you can mark this thread as answered.

                     

                    Regards,

                    Adrian

                    • 7. Re: xml to datagrid problem
                      goflashman Level 1

                      Ah Yes. Im new at forums.

                      I dont completely understand how your method works.

                       

                      item.Field[dg.columns.indexOf(column)]

                       

                      your traversing the object "item.Field"

                      What does this mean?

                       

                       

                      • 8. Re: xml to datagrid problem
                        AdrianWiecek Level 3

                        dg.columns is an array of all DataGridColumns in the datagrid.

                        indexOf(column) returns an index of currently processed column (reference to current column is passed as second argument to label function).

                         

                        This way you know which datagrid column you are processing (0 means first and so on...).

                         

                        item.Field[0] returns the text between first <Field> </Field> tags.

                         

                        Regards,

                        Adrian