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

    Dynamic DataGrid when XML is unkown



      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()">



                  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



                          //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");



                          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;




                          dgReport.columns = dgCols;                           

                          dgReport.dataProvider = xmlList;






                  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:XML id="xmlDP" source="data/employees2.xml" />

          <mx:VBox id="vbox">


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







      Simple xml file employees2.xml:


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