Converting Hierarchical Data to XML in Flex -


how convert flat/hierarchical data xml format in flex. following hierarchical data:(table format)

asia  india  chennai  tn  category1 product1 100 asia  india  mumbai   mh  category1 product1 100 asia  india  calcutta ct  category1 product1 100 asia  india  calcutta ct  category2 product2 200 emea  uk     london   ln  category3 product1 123     emea  uk     london   ln  category3 product2 455     emea  uk     reading  rn  category1 product1 500     emea  uk     reading  rn  category1 product2 430          need format/convert xml format can populate resulting xml dataprovider tree control.        asia          india              chennai                 tn                   category1                      product1                           100              mumbai                 mh                   category1                      product1                           100                                    such tree structure.                       

i make simple converter real xml structure. complexity of solution depends on source text have.

here implementation.

suppose hierarchical data structured , each element has amount of whitespaces before. in case use 4 whitespaces 1 level shift.

i load data text file, looks this:

    asia         india             chennai                 tn                     category1                         product1                             100             mumbai                 mh                     category2                         product2                             200                     category3                         product3                             300                                           product4                             400 

then go through each string , analyse level. resulting tree looks this:

enter image description here

// application

<?xml version="1.0" encoding="utf-8"?> <s:application xmlns:fx="http://ns.adobe.com/mxml/2009"             xmlns:s="library://ns.adobe.com/flex/spark"             xmlns:mx="library://ns.adobe.com/flex/mx"             minwidth="955" minheight="600" creationcomplete="init(event)"> <fx:script>     <![cdata[         import mx.events.flexevent;          private const spacedelimiter:int = 4;         private var loader:urlloader;         private var ar:array = new array();         [bindable]private var data:xml = <root/>;         private var inputstr:string;          protected function init(event:flexevent):void         {             loader = new urlloader(new urlrequest("com/treexml/tree.txt"));             loader.dataformat = urlloaderdataformat.text;             loader.addeventlistener(event.complete, completehandler);         }          private function completehandler(event:event):void         {             inputstr = urlloader(event.target).data;             parsestring();         }          private function parsestring():void         {             var levels:array = new array();             ar = inputstr.split("\r\n");             var reg:regexp = /[a-z0-9]/gi;              var globalshift:int = string(ar[0]).search(reg);              (var i:int = 0; < ar.length; i++)             {                 var item:string = ar[i];                 var shift:int = item.search(reg); //amount of witespaces before text                 var level:int = (shift - globalshift)/spacedelimiter; //level of node                 var label:string = item.substring(shift, item.length);                  levels[level] = i; //id of last element given level                   var node:xml = new xml();                 node = <child id = {i} label = {label}/>;                  if (level == 0)                     data.appendchild(node);                 else                     data..child.(@id == levels[level - 1]).appendchild(node);             }         }      ]]> </fx:script>  <mx:tree width="250"           height="400"           dataprovider="{data.child}"          labelfield="@label"/>  </s:application> 

//edit

if data not aligned means of whitespaces, try use edition of parsestring function.

suppose data looks like:

asia india chennai tn category1 product1 100 asia india mumbai mh category1 product1 100 asia india calcutta ct category1 product1 100 asia india calcutta ct category2 product2 200 emea uk london ln category3 product1 123 emea uk london ln category3 product2 455 emea uk reading rn category1 product1 500 emea uk reading rn category1 product2 430 

the function is:

            private function parsestring():void {     ar = inputstr.split("\r\n");     var map:dictionary = new dictionary();     var delimiter:string = "***";     var id:int = 0;     (var i:int = 0; < ar.length; i++)     {         if(ar[i].length){//if it's not empty string             var itemarray:array = ar[i].replace(/\s{2,}/g, ' ').split(" ");//collapse multiple spaces 1 using regex             var key:string = "";             var prevkey:string = "";              (var j:int = 0; j< itemarray.length; j++)             {                 prevkey = key;                 key += itemarray[j] + delimiter;                  if (map[key] == null)                 {                     map[key] = id;                      var node:xml = <child id = {id} label = {itemarray[j]}/>;                      if (j == 0)                         data.appendchild(node);                     else                         data..child.(@id == map[prevkey]).appendchild(node);                      id++;                 }             }         }     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -