Flex的xml解析

下面是castor转换javabean得到的一个xml文件。

我们用flex解析之。

<?xml version="1.0" encoding="UTF-8"?>
<route from="from" to="to"><flight carrier="AR1" depart="6:23a1" arrive="8:42a1" number="426"/><flight carrier="AR2" depart="6:23a2" arrive="8:42a2" number="426"/></route>

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()" xmlns:shared="components.shared.*" height="100%">
	<mx:Metadata>
		[Event(name = "totalIDListClickEvent", type = "classes.TotalIDListClickEvent")]
	</mx:Metadata>

	<mx:Script>
		<![CDATA[
			//import classes.xmlSocket;
			import mx.rpc.events.FaultEvent;
			//import classes.SelectedPageChangeEvent;
			//import classes.TotalIDListClickEvent;
			import mx.events.ListEvent;
			import mx.events.ModuleEvent;
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;
			//import classes.PageNames;
			import flash.net.XMLSocket;
			
			import mx.managers.CursorManager;
			private var socket:flash.net.XMLSocket = new flash.net.XMLSocket();

			//---------------------------------------------------------------//
			//
			//---------------------------------------------------------------//

			// Stores the returned XML data for ID list.
			[Bindable] private var _receivedIDList: ArrayCollection;

			// Clicked initial letter
			[Bindable] private var _selectedInitialLetter: String;

			// Total page number
			[Bindable] private var _totalPageNum: Number;
			// from and to
			[Bindable] private var _from: String;
			[Bindable] private var _to: String;
			// Selected page
			[Bindable] private var _selectedPage: Number = 1;

			//---------------------------------------------------------------//
			//
			//---------------------------------------------------------------//
			//---------------------------------------------------------------//

			//---------------------------------------------------------------//
			private function init(): void
			{
				_selectedInitialLetter = "A";
				httpTotalIDList.cancel();
				httpTotalIDList.send();
			}

			//---------------------------------------------------------------//
			private function idInitialLetterClicked(evt: Event): void
			{
				//Alert.show(evt.target.label, "Info");
				_selectedInitialLetter = evt.target.label;

				httpTotalIDList.cancel();
				httpTotalIDList.send();
			}

			//---------------------------------------------------------------//
			/*
			private function idListClicked(evt: ListEvent): void
			{
				//Alert.show(evt.target.selectedItem.string, "Info");
				var evtObj: TotalIDListClickEvent =
					new TotalIDListClickEvent(TotalIDListClickEvent.TOTAL_ID_LIST_CLICK_EVENT,
											  evt.target.selectedItem.string);
				dispatchEvent(evtObj);
			}
			*/

			//---------------------------------------------------------------//
			private function getHTTPResult_TotalIDList(evt: ResultEvent): void
			{
				try
				{
					_receivedIDList = evt.result.route.flight;
				}
				catch(err: TypeError)
				{
					var tempList: ArrayCollection = new ArrayCollection();
					tempList.addItem(evt.result.root.searchResult);
					_receivedIDList = tempList;
				}

				_from = evt.result.route.from;
				Alert.show("from is " + _from, "信息");
				_to = evt.result.route.to;
				Alert.show("to is " + _to, "信息");
				if(this._totalPageNum==1||this._totalPageNum==0)
				{
	 		 		btnToForword.enabled=false;
	 		 		btnToLatter.enabled=false;
				}else {
					if(this.combPageNums.selectedIndex == 0 || this.combPageNums.selectedIndex==-1)
		 		 	{
	 			 		btnToForword.enabled=false;
	 			 	}else {
	 			 		btnToForword.enabled=true;
	 			 	}
	 			 	if(this.combPageNums.selectedIndex == this._totalPageNum-1)
	 			 	{
	 			 		btnToLatter.enabled=false;
	 			 	}else{
	 			 		btnToLatter.enabled=true;
	 			 	}
	 			 	if(this.combPageNums.selectedIndex != 0&&
	 			 	this.combPageNums.selectedIndex != -1&&
	 			 	this.combPageNums.selectedIndex != this._totalPageNum-1)
	 			 	{
	 			 		btnToForword.enabled=true;
	 			 		btnToLatter.enabled=true;
	 			 	}
				}

				var i: Number;
				pagesInComboBox.removeAll();
				for(i = 1; i < _totalPageNum + 1; i ++)
				{
					pagesInComboBox.addItem(i.toString());
				}
	 		 	combPageNums.selectedIndex = _selectedPage - 1;

				if(_receivedIDList == null)
				{
					//Alert.show("没有找到以" + _selectedInitialLetter + "开头的ID", "信息");
					return;
				}
			}

			//---------------------------------------------------------------//
			
			private function handleHTTPFault(evt: FaultEvent): void
			{
				Alert.show("在获取ID列表时发生了错误:" + "\n" + evt.message, "错误");
			}
			


			private function selectedPageChanged(): void
			{
				_selectedPage = combPageNums.selectedIndex + 1;
				httpTotalIDList.cancel();
				httpTotalIDList.send();
			}
		[Bindable] private var pagesInComboBox: ArrayCollection = new ArrayCollection();
  	    [Bindable] private var Forword:String = "<<";
 	    [Bindable] private var Latter:String = ">>";
			private function goToForword():void
 		    {
 			  this.combPageNums.selectedIndex -= 1;
 	 		  selectedPageChanged();
 		     }

 		     private function goToLatter():void
 		     {
 		 	   this.combPageNums.selectedIndex +=1;
 		 	   selectedPageChanged();
 		     }
		]]>
	</mx:Script>

	<mx:ArrayCollection id="alphabetHead">
		<mx:String>A</mx:String>
		<mx:String>B</mx:String>
		<mx:String>C</mx:String>
		<mx:String>D</mx:String>
		<mx:String>E</mx:String>
		<mx:String>F</mx:String>
		<mx:String>G</mx:String>
		<mx:String>H</mx:String>
		<mx:String>I</mx:String>
		<mx:String>J</mx:String>
		<mx:String>K</mx:String>
		<mx:String>L</mx:String>
		<mx:String>M</mx:String>
	</mx:ArrayCollection>
	<mx:ArrayCollection id="alphabetEnd">
		<mx:String>N</mx:String>
		<mx:String>O</mx:String>
		<mx:String>P</mx:String>
		<mx:String>Q</mx:String>
		<mx:String>R</mx:String>
		<mx:String>S</mx:String>
		<mx:String>T</mx:String>
		<mx:String>U</mx:String>
		<mx:String>V</mx:String>
		<mx:String>W</mx:String>
		<mx:String>X</mx:String>
		<mx:String>Y</mx:String>
		<mx:String>Z</mx:String>
	</mx:ArrayCollection>

	<!--mx:HTTPService id="httpTotalIDList" method="GET" result="getHTTPResult_TotalIDList(event)" fault="handleHTTPFault(event)" url="{PageNames.siteRoot}{PageNames.IDListPageName}">
		<mx:request xmlns="">
		    <turnToPage>{_selectedPage}</turnToPage>
			<head>{_selectedInitialLetter}</head>
			<protocolType>0</protocolType>
			<format>{"xml"}</format>
		</mx:request>
	</mx:HTTPService-->
	<mx:HTTPService id="httpTotalIDList" method="GET" result="getHTTPResult_TotalIDList(event)" fault="handleHTTPFault(event)" url="http://127.0.0.1:8080/test.xml">
		<mx:request xmlns="">
		    <turnToPage>{_selectedPage}</turnToPage>
			<head>{_selectedInitialLetter}</head>
			<protocolType>0</protocolType>
			<format>{"xml"}</format>
		</mx:request>
	</mx:HTTPService>
	<mx:VBox height="100%">
	<mx:HBox>
	<mx:VBox height="100%">
		<mx:Repeater id="alphabetRepeaterH" dataProvider="{alphabetHead}">
			<mx:LinkButton id="alphabetSelectorH" label="{alphabetRepeaterH.currentItem}" click="idInitialLetterClicked(event)"/>
		</mx:Repeater>
	</mx:VBox>
	<mx:VBox height="100%">
		<mx:Repeater id="alphabetRepeaterE" dataProvider="{alphabetEnd}">
			<mx:LinkButton id="alphabetSelectorE" label="{alphabetRepeaterE.currentItem}" click="idInitialLetterClicked(event)"/>
		</mx:Repeater>
	</mx:VBox>
	</mx:HBox>
	</mx:VBox>

	<mx:VBox width="100%" height="100%">
		<mx:DataGrid id="idList" dataProvider="{_receivedIDList}" dragEnabled="true" width="100%" height="100%" buttonMode="true" useHandCursor="true">
			<mx:columns>
				<mx:DataGridColumn dataField="carrier" headerText="carrier"/>
			</mx:columns>
		</mx:DataGrid>
		<mx:HBox width="50%">
			<mx:Label id="lableTotalPages" text="{'共'}{_totalPageNum}{'页'}"/>
			<mx:Button id="btnToForword" label="{Forword}"  click="goToForword()" toolTip="前一页" enabled="false" width="1%" buttonMode="true" useHandCursor="true" />
			<mx:ComboBox id="combPageNums" dataProvider="{pagesInComboBox}" change="selectedPageChanged()" textAlign="center" width="55" openDuration="100" closeDuration="100" buttonMode="true" useHandCursor="true"/>
			<mx:Button  id="btnToLatter" label="{Latter}"  click="goToLatter()" toolTip="后一页" enabled="false" width="1%" buttonMode="true" useHandCursor="true"/>
		</mx:HBox>
	</mx:VBox>
</mx:Application>

 

 

 

你可能感兴趣的:(.net,xml,socket,Flex,Flash)