0 Replies Latest reply on Jun 19, 2009 3:01 AM by karthik flex

    Read xml to datagrid?

    karthik flex

      hi guys

       

      i have some doubt i have one xml

       

      <root>
      <data>

      <time datas="Hello">

      <john second="Karthikkkkkkkkk"></john>

      </time>
      </data>
      </root>

       

      can i make "datas" as header text and "second" as cell data in datagrid

       

      private function handleResult(event:ResultEvent):void
      {   
           xmlResult = XML(event.result);    
           callLater(initApp);
      }

       

      public function initApp():void
      {
          _xlcCatalog = new XMLListCollection(xmlResult.data); 
          buildDG();                                                   
      }

      private function buildDG():void
      {
          var aColumnDef:Array = getColumnDefArray();                 
          var oColumnDef:Object;  
          var dgc:DataGridColumn;
          var aColumnsNew:Array = dg.columns
          var iTotalDGWidth:int = 0;  
          for (var i:int=0;i<aColumnDef.length;i++) 
          {                                                         
            oColumnDef = aColumnDef[i];
           
            dgc = new DataGridColumn();                           
            dgc.dataField = oColumnDef.dataField;
            dgc.width = oColumnDef.width;
            iTotalDGWidth += dgc.width;                                 
            dgc.editable = oColumnDef.editable;
            dgc.sortable = oColumnDef.sortable
            dgc.visible = oColumnDef.visible;
            dgc.wordWrap = oColumnDef.wordWrap;
            aColumnsNew.push(dgc)                                       
          }  
         // dg.draggableColumns=false;
         // dg.resizableColumns=false;
          dg.editable=true;
          dg.columns = aColumnsNew;                                      
          dg.editable = true;
          dg.width = iTotalDGWidth;
          dg.rowCount=_xlcCatalog.length;
         // trace("_xlcCatalog ",_xlcCatalog.length);
          dg.dataProvider = _xlcCatalog; 
                                          
          this.addChild(dg);                                                                                                                                                                       
      }

       

      private function getColumnDefArray():Array
      {    
      //  Alert.show("colcount:" + _xlcCatalog.toXMLString());
          var aColumns:Array = new Array();
          var node0:XML = xmlResult.data[0];   
          trace(xmlResult);         
          var xlColumns:XMLList = node0.children();                       
          var xmlColumn:XML;
          var oColumnDef:Object;
          for (var i:int=0;i<xlColumns.length();i++)  {                   
            xmlColumn = xlColumns[i];
            oColumnDef = new Object();
            oColumnDef.dataField = xmlColumn.localName();     
            switch (oColumnDef.dataField)  {
                /* case "time":         
                  oColumnDef.width = 80;
                oColumnDef.sortable = false;
                oColumnDef.visible = true;
                oColumnDef.editable = false;
                oColumnDef.wordWrap = true;        
                break; */
              default:
                oColumnDef.width = 150;
                oColumnDef.sortable = true;
                oColumnDef.visible = true;
                oColumnDef.editable = false;
                oColumnDef.wordWrap = true;         
                break;                           
            }
            aColumns.push(oColumnDef);
          }
          return aColumns;                                                   
      }

       

      if i change oColumnDef.dataField = xmlColumn.localName(); (it is taking the node name e.g time, john)  but when   to oColumnDef.dataField = xmlColumn.attribute();   it is taking the node attributes(eg Hello, Karthikkkkkkkkk) but not displaying one below the other

       

      any idea

       

      karthik.k