FLEX 中的XML,XMLList,XMLListCollection类练习

留个笔记
<?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" 
			    initialize="init()" minWidth="955" minHeight="600" width="497" height="538">
	<fx:Script>
		<![CDATA[
			import mx.collections.Sort;
			import mx.collections.SortField;
			import mx.collections.XMLListCollection;
			
			private var _projectsXML:XML=
			<projects>
				<project id="1" name="pro1">
					<task id="1">
						<name>UnderStand E4X</name>
						<notes>Cool, for XML anyway</notes>
					</task>
					<task id="2">
						<name>Learn XMLListCollection</name>
						<notes>simple</notes>
					</task>
				</project>
				<project id="2" name="pro2">
					<task id="3">
						<name>Learn XMLList</name>
					</task>
					<task id="4">
						<name>Get a coffee</name>
						<notes>Very necessary</notes>
					</task>
				</project>
			</projects>
				
			private function init():void{
				var output:String = "";
				output += "Full XML:\n" + _projectsXML;
				
				output += "\n\nUsing E4X and XMLList:\n";
				output += _projectsXML.project[0].task[0].name + "\n";
				output += _projectsXML.project.(@name=="pro2").task.(@id==3).name + "\n";
				
				var projects:XMLList = _projectsXML.children();
				for each(var project:XML in projects){
					output += "Project:" + project.@name + "\n";
					for each(var task:XML in project.task){
						output += "  Task:" + task.@id + ": " + task.name;
						if(task.hasOwnProperty('notes')){
							output += "(" + task.notes + ")";
						}
						output += "\n";
					}
					//output += "\n";
				}
				
				output += "\n\nLearning XMLListCollection and Sorting:\n";
				var allTasks:XMLListCollection = new XMLListCollection(_projectsXML.descendants("task"));
				var sort:Sort = new Sort();
				sort.fields =[ new SortField("name",true)];
				allTasks.sort = sort;
				allTasks.refresh();
				
				output += "\n\n\n" + allTasks + "\n\n\n";
				
				for each(var sortedTask:XML in allTasks){
					output += sortedTask.name + "\n";
				}

				outputArea.text = output;
			}
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:TextArea id="outputArea" x="0" y="0" width="100%" height="100%"/>
</s:Application>

你可能感兴趣的:(Collection)