'xml'에 해당되는 글 1건

  1. 2009.02.02 ArrayCollection -> XML -> Tree Menu

ArrayCollection 데이터를 xml data로 생성하고 이를 Tree 컴퍼넌트에 물려준다..

-------------------------------------------------------------------------------------------------+

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 applicationComplete="init ()">
 
 <mx:Script>
  <![CDATA[
   import mx.utils.XMLUtil;
   import mx.collections.ArrayCollection;
   var arrData :ArrayCollection = new ArrayCollection([
    {categorycd:"1",parentcd:"0",nm:"그룹홍보",level:"1"},
    {categorycd:"3",parentcd:"0",nm:"SMS발송(old)",level:"1"},
    {categorycd:"5",parentcd:"0",nm:"기타1",level:"1"},
    {categorycd:"2",parentcd:"1",nm:"1234",level:"2"},
    {categorycd:"8",parentcd:"1",nm:"12341",level:"2"},
    {categorycd:"4",parentcd:"3",nm:"그룹관련12",level:"2"},
   ]);
  
   var resultXML :XML = <root categorycd="0" nm="루트" />;
  
   private function init () :void
   {
    convertToXML ( arrData );
   }
  
   private function convertToXML ( arr :ArrayCollection ) :void
   {
    var len :int = arr.length;
    for ( var i:int=0; i<len; i++ )
    {
     insertItem (arr.getItemAt(i).parentcd, toXML(arr.getItemAt(i)), new XMLList(resultXML));
    }
    trace ( resultXML.toXMLString() );
   
    treeMenu.dataProvider = resultXML;
   }
  
   private function insertItem(_parentId : String, _child : XML, _xml :XMLList) : Boolean
   {
      var parentElement : XMLList = findXMLList(_parentId, _xml);
      if (parentElement == null)
         return false;
  
      parentElement.* += _child;
      return true;
   }
  
   private function findXMLList (_id : String, _xml : XMLList) : XMLList
         {
            if (_xml.@categorycd == _id)
               return _xml;
       
            var children : XMLList = _xml.elements ();
            var result : XMLList = null;
          
            for each (var x : XML in children)
            {
               result = arguments.callee (_id, new XMLList(x));
               if (result != null)
                  return result;
            }
       
            return null;
         }
  
   private function toXML ( obj :Object, tagName:String="node" ) :XML
   {
    var return_xml :XML = new XML("<"+tagName+"/>");
    for ( var key:String in obj )
    {
     return_xml.@[key] = obj[key];
    }
    return return_xml;
   }
  
  
  ]]>
 </mx:Script>
 
 <mx:Tree id="treeMenu" width="300" height="100%" labelField="@nm"
  showRoot="false">
 
 </mx:Tree>
 
</mx:Application>

-------------------------------------------------------------------------------------------------+

ArrayCollection 에 들어있는 데이터를 뽑아다가 xml 로 만들어주고
이를 treeMenu.dataProvider = resultXML 로 데이터를 물려주는거다..
하나하나 보면 어려운 부분이 없다..
그냥 재미있길래 ㅎ

출처 - http://cafe.naver.com/flexcomponent/1429
Posted by 복태아빠
,