To my knowledge there's not a simple plug and play method for
accomplishing what you want to do, so you'll have to perform some data
transformation to get what you want.

Luckily, when you're dealing with XML data, this is fairly easy thanks
to E4X. What you'll have to do is write a some code that parses out
the 'structure' section of your XML, uses it to generate the
DataGridColumn components, then adds them to the DataGrid. You'll then
need to parse out your data and transform it into some format that the
datagrid can easily use.

The code below should help get you started

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
        xmlns:mx="http://www.adobe.com/2006/mxml"; 
        layout="absolute" 
        creationComplete="onCreationComplete()">

        <mx:Script>
                <![CDATA[
                        import mx.controls.dataGridClasses.DataGridColumn;
                        import mx.controls.Alert;
                        protected var xml:XML = 
                                <xml>
                                
                                        <adml version="0">

                                                <structure>
                                                        <header>Name</header>
                                                        <header>Address</header>
                                                        <header>Phone</header>
                                                </structure>
                                                
                                                <contact>
                                                        <data>Joe Doe</data>
                                                        <data>Anywhere 
USA</data>
                                                        
<data>555-555-5555</data>
                                                </contact>
                                                
                                                <contact>
                                                        <data>Jane Doe</data>
                                                        <data>Anywhere 
Canada</data>
                                                        
<data>555-123-4567</data>
                                                </contact>
                                                
                                        </adml>
                                        
                                </xml>;
                
                        [Bindable]
                        protected var dgColumns:Array = [];
                        
                        [Bindable]
                        protected var dgData:Array = [];
                        
                        protected function onCreationComplete():void
                        {
                                var newCol:DataGridColumn;
                                var newColArray:Array = [];
                                var newDataArray:Array = [];
                                var newDataObject:Object;
                                var colCounter:int = 0;
                                
                                for each(var headerName:String in 
xml.adml.structure.header)
                                {
                                        newCol = new DataGridColumn();
                                        newCol.headerText = headerName;
                                        newCol.visible = true;
                                        newCol.dataField = "d" + 
colCounter.toString();
                                        newColArray.push(newCol);
                                        
                                        colCounter++;
                                }
                                
                                dgColumns = newColArray;
                                
                                for each(var contactXml:XML in xml.adml.contact)
                                {
                                        colCounter = 0;
                                        newDataObject = {};
                                        
                                        for each(var dataText:String in 
contactXml.data)
                                        {
                                                newDataObject["d" + 
colCounter.toString()] = dataText;
                                                colCounter++;
                                        }
                                        
                                        newDataArray.push(newDataObject);
                                }
                                
                                dgData = newDataArray;
                        }
                        
                ]]>
        </mx:Script>
        
        <mx:DataGrid 
                x="0" 
                y="0" 
                width="100%" 
                height="100%" 
                columns="{dgColumns}" 
                dataProvider="{dgData}"/>
                
</mx:Application>


--- In [email protected], "atlas1j" <[EMAIL PROTECTED]> wrote:
>
> This is probably simple for those that aren't flex newbies but my 
> flex books haven't arrived yet from Amazon and I am getting confused 
> by reading and searching on the web. What I have are xml document 
> that I don't control that describe their own structure. See the 
> simplified sample below:
> 
> <xml>
> <adml version="0">
> 
> <structure>
> <header>Name</header>
> <header>Address</header>
> <header>Phone</header>
> </structure>
> 
> <contact>
> <data>Joe Doe</data>
> <data>Anywhere USA</data>
> <data>555-555-5555</data>
> </contact>
> 
> <contact>
> <data>Joe Doe</data>
> <data>Anywhere USA</data>
> <data>555-555-5555</data>
> </contact>
> 
> I want to make the <structure> elements the data grid column headers 
> and have each of the <data> items rendered in the correct column.  
> Can anyone provide a pointer as to how to proceed?  Thanks!
>


Reply via email to