4 Replies Latest reply on Apr 17, 2009 10:00 AM by Gregory Lafrance

    Having trouble parsing external XML file into DataGrid component in Flex 3

      Having trouble parsing an external XML file into my Flex 3 app.

       

      Here is my MXML for my Flex 3 app:

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

      xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="contactService.send()">

       

          <mx:Script>

              <![CDATA[

                  import mx.printing.*;

       

                  // Create a PrintJob instance.

                  private function doPrint():void {

                      // Create an instance of the FlexPrintJob class.

                      var printJob:FlexPrintJob = new FlexPrintJob();

       

                      // Start the print job.

                      if (printJob.start() != true) return;

       

                      // Add the object to print. Do not scale it.

                      printJob.addObject(contactsDataGrid, FlexPrintJobScaleType.NONE);

       

                      // Send the job to the printer.

                      printJob.send();

                  }

              ]]>

          </mx:Script>

       

      <mx:DataGrid id="contactsDataGrid"

      dataProvider="{contactsService.lastResult.contact}" height="726">

      <mx:columns>

      <mx:DataGridColumn headerText="Product Name" dataField="Productname" />

      <mx:DataGridColumn headerText="Architecture" dataField="Architecture" />

      <mx:DataGridColumn headerText="CPU Speed" dataField="CPU_Speedunit" />

      <mx:DataGridColumn headerText="Memory Type" dataField="Memory_Type" />

      <mx:DataGridColumn headerText="Program Memory" dataField="Program_Memoryunit" />

      <mx:DataGridColumn headerText="RAM Bytes" dataField="RAM_Bytes" />

      <mx:DataGridColumn headerText="Temp. Range Unit" dataField="Temperature_Rangeunit" />

      <mx:DataGridColumn headerText="Operating Voltage" dataField="Operating_Voltage_Rangeunit" />

      <mx:DataGridColumn headerText="Pin Count" dataField="Pin_Count" />

      <mx:DataGridColumn headerText="System Management" dataField="System_Management_Features" />

      <mx:DataGridColumn headerText="Internal Oscillators" dataField="Internal_Oscillator" />

      <mx:DataGridColumn headerText="nanoWatt Features" dataField="nanoWatt_Features" />

      <mx:DataGridColumn headerText="Digital Communication Peripherals" dataField="Digital_Communication_Peripherals" />

      <mx:DataGridColumn headerText="Analog Peripherals" dataField="Analog_Peripherals" />

      </mx:columns>

      </mx:DataGrid>

      <mx:Button id="printcard"

                  label="Print"

                  click="doPrint();"/>

       

      <mx:XML id="contactsXML" xmlns="" source="contacts.xml"

      </mx:XML>

       

      <mx:HTTPService id="contactsService"

      resultformat="e4x"

      url="contacts.xml"

      </mx:HTTPService>

      </mx:Application>

       

       

      And here is my XML that I am trying to parse:

       

       

      <mx:XML id="contactsXML: xmlns="">

      <Products>

      <Productname>+"dsPIC30F4012"</Productname>

      <Architecture>="16-bit"</Architecture>

      <CPU_Speedunit>="MIPS 3.00"</CPU_Speedunit>

      <Memory_Type>="Flash"</Memory_Type>

      <Program_Memoryunit>="KB 4.80"</Program_Memoryunit>

      <RAM_Bytes>"2,048"</RAM_Bytes>

      <Temperature_Rangeunit>="C -40 to 125"</Temperature_Rangeunit>

      <Operating_Voltage_Rangeunit>="V 2.5 to 5.5"</Operating_Voltage_Rangeunit>

      <Pin_Count>"28"</Pin_Count>

      <System_Management_Features>"PBOR, LVD"</System_Management_Features>

      <Internal_Oscillator>"7.37 MHz, 512 kHz"</Internal_Oscillator>

      <nanoWatt_Features>"Fast Wake/Fast Control"</nanoWatt_Features>

      <Digital_Communication_Peripherals>"1-UART, 1-SPI, 1-I2C"</Digital_Communication_Peripherals>

      <Analog_Peripherals>"1-A/D 6x10-bit @ 1000(ksps)"</Analog_Peripherals>

      </Products>

      </mx:XML>

        • 1. Re: Having trouble parsing external XML file into DataGrid component in Flex 3
          Gregory Lafrance Level 6

          Several things are wrong with your code:

           

          1) contactsService.lastResult.contact     but your data has no "contact" node

           

          2) Your XML data ends with     </mx:XML>   but I don't think that should be there, <mx:XML id="contactsXML: xmlns=""> is just the XML file header.

           

          3) Your data should be structured like this:

           

          <Products>

              <Product>

                  <Productname>+"dsPIC30F4012"</Productname>

                  <Architecture>="16-bit"</Architecture>

                  <CPU_Speedunit>="MIPS 3.00"</CPU_Speedunit>

                  <Memory_Type>="Flash"</Memory_Type>

                  <Program_Memoryunit>="KB 4.80"</Program_Memoryunit>

                  <RAM_Bytes>"2,048"</RAM_Bytes>

                  <Temperature_Rangeunit>="C -40 to 125"</Temperature_Rangeunit>

                  <Operating_Voltage_Rangeunit>="V 2.5 to 5.5"</Operating_Voltage_Rangeunit>

                  <Pin_Count>"28"</Pin_Count>

                  <System_Management_Features>"PBOR, LVD"</System_Management_Features>

                  <Internal_Oscillator>"7.37 MHz, 512 kHz"</Internal_Oscillator>

                  <nanoWatt_Features>"Fast Wake/Fast Control"</nanoWatt_Features>

                  <Digital_Communication_Peripherals>"1-UART, 1-SPI, 1-I2C"</Digital_Communication_Peripherals>

                  <Analog_Peripherals>"1-A/D 6x10-bit @ 1000(ksps)"</Analog_Peripherals>

              </Product>

          </Products>

           

           

          And parsed like this:    contactsService.lastResult.product

           

          Better yet, don't use lastResult, use a result handler function.

          1 person found this helpful
          • 2. Re: Having trouble parsing external XML file into DataGrid component in Flex 3
            Level 1

            Greg,

             

            My apologies for my belated response. Thanks for your help. Your suggestions definitely fixed the XML issues. Still having trouble with the app parsing the XML though into Flex an displaying the content into the DataGrid Am able to see the datagrid but getting  a big red X in a box instead of datagrid displaying data.

            • 3. Re: Having trouble parsing external XML file into DataGrid component in Flex 3
              Level 1

              Here is my current Flex 3 MXML:

               

              <?xml version="1.0" encoding="utf-8"?>

              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

              applicationComplete="ProductService.send()">

               

              <mx:DataGrid id="ProductsDataGrid"

              dataProvider="{ProductsService.lastResult.product}">

              <mx:columns>

              <mx:DataGridColumn headerText="Product Name" dataField="Productname"/>

              <mx:DataGridColumn headerText="Architecture" dataField="Architecture"/>

              <mx:DataGridColumn headerText="CPU Speed" dataField="CPU_Speedunit"/>

              <mx:DataGridColumn headerText="Memory Type" dataField="Memory_Type"/>

              <mx:DataGridColumn headerText="Program Memory" dataField="Program_Memoryunit"/>

              <mx:DataGridColumn headerText="RAM Bytes" dataField="RAM_Bytes"/>

              <mx:DataGridColumn headerText="Temp. Range Unit" dataField="Temperature_Rangeunit"/>

              <mx:DataGridColumn headerText="Operating Voltage" dataField="Operating_Voltage_Rangeunit"/>

              <mx:DataGridColumn headerText="Pin Count" dataField="Pin_Count"/>

              <mx:DataGridColumn headerText="System Management" dataField="System_Management_Features"/>

              <mx:DataGridColumn headerText="Internal Oscillators" dataField="Internal_Oscillator"/>

              <mx:DataGridColumn headerText="nanoWatt Features" dataField="nanoWatt_Features"/>

              <mx:DataGridColumn headerText="Digital Communication Peripherals" dataField="Digital_Communication_Peripherals"/>

              <mx:DataGridColumn headerText="Analog Peripherals" dataField="Analog_Peripherals" />

              </mx:columns>

              </mx:DataGrid>

               

              <mx:dataProvider>

              <mx:XML id="ProductsXML" xmlns="" source="Products.xml" />

              <mx:HTTPService id="e4x"

              url="Products.xml"/>

              </mx:dataProvider>

              </mx:Application>

              Line 27 is where my issues are. The errror is 'Parse error at HTTPService id="e4x"'

              • 4. Re: Having trouble parsing external XML file into DataGrid component in Flex 3
                Gregory Lafrance Level 6

                Your code still has problems:

                <mx:HTTPService id="e4x" url="Products.xml"/>

                 

                1) Your code has ProductService.send() but your HTTPService id is e4x.

                2) You should define your HTTPService like this:

                 

                <mx:HTTPService id="ProductService" refultFormat="e4x" url="Products.xml"/>

                 

                Please read the Flex Builder help sys pages on HTTPService and processing results. It is better to use a result handler, and not use lastResult.