package castortest; public class FlightBean { private String m_carrier; private int m_number; private String m_departure; private String m_arrival; public FlightBean() { } public void setCarrier(String carrier) { m_carrier = carrier; } public String getCarrier() { return m_carrier; } public void setNumber(int number) { m_number = number; } public int getNumber() { return m_number; } public void setDepartureTime(String time) { m_departure = time; } public String getDepartureTime() { return m_departure; } public void setArrivalTime(String time) { m_arrival = time; } public String getArrivalTime() { return m_arrival; } }
package castortest; import java.util.ArrayList; public class Routebean { private String m_from; private String m_to; private ArrayList m_flights; public Routebean() { m_flights = new ArrayList(); } public void setFrom(String from) { m_from = from; } public String getFrom() { return m_from; } public void setTo(String to) { m_to = to; } public String getTo() { return m_to; } public ArrayList getFlights() { return m_flights; } public void addFlight(FlightBean flight) { m_flights.add(flight); } }
package castortest; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.io.Writer; import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.Unmarshaller; public class RouteBeanTest2 { public static void main(String[] argv) { // build a test bean // FlightBean bean1 = new FlightBean(); bean1.setCarrier("AR1"); bean1.setNumber(426); bean1.setDepartureTime("6:23a1"); bean1.setArrivalTime("8:42a1"); // FlightBean bean2 = new FlightBean(); bean2.setCarrier("AR2"); bean2.setNumber(426); bean2.setDepartureTime("6:23a2"); bean2.setArrivalTime("8:42a2"); // Routebean bean = new Routebean(); bean.setFrom("from"); bean.setTo("to"); bean.addFlight(bean1); bean.addFlight(bean2); try { // write it out as XML (if not already present) File file = new File("test.xml"); Writer writer = new FileWriter(file); Marshaller.marshal(bean, writer); // Marshaller marshaller = new Marshaller(writer); // marshaller.marshal(bean, writer); // // now restore the value and list what we get Reader reader = new FileReader(file); Routebean read = (Routebean) Unmarshaller.unmarshal( Routebean.class, reader); FlightBean read1 = (FlightBean) read.getFlights().get(0); FlightBean read2 = (FlightBean) read.getFlights().get(1); System.out.println("Flight " + read1.getCarrier() + read1.getNumber() + " departing at " + read1.getDepartureTime() + " and arriving at " + read1.getArrivalTime()); System.out.println("Flight " + read2.getCarrier() + read2.getNumber() + " departing at " + read2.getDepartureTime() + " and arriving at " + read2.getArrivalTime()); System.out.println(read.getFrom()); System.out.println(read.getTo()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
当使用 castor ,而我们又懒得不想写 castor 的 map 文件,恰恰这时候 javabean 里还持有一个 list ,这个时候会生成什么样的 xml ? 当使用 castor ,而我们又懒得不想写 castor 的 map 文件,恰恰这时候 javabean 里还持有一个 list ,这个时候会生成什么样的 xml ?
<?xml version="1.0" encoding="UTF-8" ?> - <routebean> - <flights xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" number="426" xsi:type="java:castortest.FlightBean"> <arrival-time>8:42a1</arrival-time> <departure-time>6:23a1</departure-time> <carrier>AR1</carrier> </flights> - <flights xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" number="426" xsi:type="java:castortest.FlightBean"> <arrival-time>8:42a2</arrival-time> <departure-time>6:23a2</departure-time> <carrier>AR2</carrier> </flights> <to>to</to> <from>from</from> </routebean>
生成这样的xml,flex是否可以解析?
<?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.routebean.flights; } catch(err: TypeError) { var tempList: ArrayCollection = new ArrayCollection(); tempList.addItem(evt.result.root.searchResult); _receivedIDList = tempList; } _from = evt.result.routebean.from; Alert.show("from is " + _from, "信息"); _to = evt.result.routebean.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:HTTPService id="httpTotalIDList" method="GET" result="getHTTPResult_TotalIDList(event)" fault="handleHTTPFault(event)" url="file:///C:/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="arrival-time" headerText="flights"/> <mx:DataGridColumn dataField="departure-time" headerText="flights"/> <mx:DataGridColumn dataField="carrier" headerText="flights"/> </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>