0 Replies Latest reply on Sep 22, 2009 4:50 AM by topmuffinsecret

    Dynamic DataGrid when XML is unkown

    topmuffinsecret

      Hello!

      I've got a problem with displaying xml in datagrid. After running  just column names are displayed.

       

       

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

      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">

          <mx:Script>

              <![CDATA[

                  import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;

                  import mx.controls.dataGridClasses.DataGridColumn;

                  import flash.net.URLLoader;

                  import flash.net.URLRequest;

                  import flash.events.*;

                  import flash.errors.*;

                  import mx.controls.*;

       

                  [Bindable] private var xmlList:XMLList = new XMLList();

       

                  private function init():void{

                      var xmlLoader:URLLoader = new URLLoader();

                      xmlLoader.addEventListener(Event.COMPLETE, xmlLoadComplete);

                      xmlLoader.load(new URLRequest("data/employees2.xml"));

                  }

       

                  private function xmlLoadComplete(evt:Event):void

                  {

                      try{

                          //get all the child elements of the root

                          xmlList = XML(evt.target.data).children();

                          //check to make sure there are results

                          if(xmlList.length() < 1){

                              Alert.show("There were no results that matched your search criteria");

                              return;

                          }

                          var dgCols:Array = new Array() ;

                          var xmlColumns:XMLList = xmlList;

       

                          for(var x:uint=0; x < xmlColumns.length(); x++){

                              var xml:XML = xmlColumns[x];

                              var col:DataGridColumn = new DataGridColumn();

                              col.headerText = xml.name();

                              col.dataField = xml.name();

                              col.labelFunction = scrubContent;

                              dgCols.push(col);

                          }

       

                          dgReport.columns = dgCols;                           

                          dgReport.dataProvider = xmlList;

       

                      }catch(e:Error){

                          Alert.show(e.message);   

                      }

                  }

                  private function scrubContent(item:Object, col:DataGridColumn):String{

                      var xml:XML = XML(item);

                      var df:String = col.dataField;

                      return cleanHTML(decodeURI(xml.child(df)));

                  }

                  private function cleanHTML(str:String):String

                  {

                      var pattern:RegExp = /<TEXTFORMAT.*?>/g;

                      var str:String = str.replace(pattern, "");

                      pattern = /<FONT.*?>/g;

                      str = str.replace(pattern, "");

                      pattern = /<\/FONT.*?>/g;

                      str = str.replace(pattern, "");

                      pattern = /<P.*?>/g;

                      str = str.replace(pattern, "");

                      pattern = /<\/P.*?>/g;

                      str = str.replace(pattern, "");

                      pattern = /<\/TEXTFORMAT.*?>/g;

                      str = str.replace(pattern, "");

       

                      return str;

                  }

       

       

              ]]>

          </mx:Script>

          <mx:XML id="xmlDP" source="data/employees2.xml" />

          <mx:VBox id="vbox">

       

              <mx:DataGrid id="dgReport" width="600"/>

       

          </mx:VBox>

      </mx:Canvas>

       

       

       

      Simple xml file employees2.xml:

       

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

      <row>

         <firstName>

         Joe

         </firstName>

         <lastName>

         Jackson

         </lastName>

      </row>