Flex4中动态解析XML数据并转换为ArrayCollection

在Flex4中处理xml数据,为了方便,一般采用SimpleXMLDecoder直接转换为ArrayCollection

但有时为了处理xml中的数据,特别是xml文件中包含日期型数据(Date)时,就需要自己解析XML并进行相应的处理

<?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" 
			   xmlns:amcharts="http://www.amcharts.com/com_internal"
			   backgroundColor="#FFFFFF"
			   width="100%" height="100%"
			   initialize="initializeHandler(event)">
	
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.events.FlexEvent;
			
			
			private var chartData:ArrayCollection = new ArrayCollection();

			protected function initializeHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				var myXML:XML = <areas><area state="广东省" p="1">广州</area><area state="湖北省" p="2">武汉</area></areas>;
						
				// 遍历子节点
				var rcdNum:int = myXML.child("area").length();   
				for( var i:int = 0; i < rcdNum; i++ )  					
				{
					var _item:Object = new Object();
					
					// 遍历属性
					var attNum:int = myXML.area[i].attributes().length();					
					for (var j:int=0; j<attNum; j++)
					{
						var _attName:String = myXML.area[i].attributes()[j].name();
						var _attValue:String = myXML.area[i].attribute(_attName);
					
						// 设置Object属性
						_item[_attName] = _attValue;
					}  
					
					// 添加到ArrayCollection中
					chartData.addItem(_item);
				}  			
			}

		]]>
	</fx:Script>
	
</s:Application>


你可能感兴趣的:(Flex4中动态解析XML数据并转换为ArrayCollection)