flex 操作xml

service查询出list组装成为xml

 

public static Document createXMLUsersDocument(String filePath,List<Users_rt> list){ // System.out.println("start2"); //定义一个xml对象 Document xmlDocument = DocumentHelper.createDocument() ; //可以通过OutputFormat来设置 //添加xml的根节点 Element booksElement = xmlDocument.addElement("users"); xmlDocument.setRootElement(booksElement); //添加注释 //booksElement.addComment("这是数据库中的users_rt表中的查询全部的数据") ; //向users元素添加user元素 for(int i=0;i<list.size();i++){ Users_rt users = list.get(i); Element userOne = booksElement.addElement("user"); //userOne.addAttribute("version", "1.0"); Element userOneId = userOne.addElement("id") ; userOneId.setText(users.getId()+""); Element userOneUsername = userOne.addElement("username") ; userOneUsername.setText(users.getUsername()); Element userOnePassword = userOne.addElement("password"); userOnePassword.setText(users.getPassword()); } System.out.println("xml拼接完成"); int ret = -1 ; XMLWriter writer = null ; try { // int indexOfLastSeparator = filePath.lastIndexOf(File.separator); // String directoryStr = filePath.substring(0,indexOfLastSeparator); // File directory = new File(directoryStr); // System.out.println("需要的地址是否存在"+directory.exists()); // if(!directory.exists()){ // System.out.println(directory); // directory.mkdir(); // } // String fileName = filePath.substring(indexOfLastSeparator+1); // File xmlFile = new File(directory,fileName); // System.out.println("xml文档是否存在"+xmlFile.exists()); // if(!xmlFile.exists()){ // System.out.println(xmlFile); // xmlFile.createNewFile(); // } /**** * 默认生成的XML文件排版格式比较乱,可以通过OutputFormat类的createCompactFormat()方法 * 或createPrettyPrint()方法格式化输出 * 默认采用createCompactFormat()方法,显示比较紧凑 * createPrettyPrint(),xml文档格式会变焦美观 */ // OutputFormat format = OutputFormat.createPrettyPrint(); // format.setEncoding("UTF-8"); // System.out.println("format对象"+format); // writer = new XMLWriter(new FileWriter(xmlFile),format); // writer.write(xmlDocument); ret = 0 ; } catch (Exception e) { e.printStackTrace(); }finally{ if(writer != null){ try { writer.close(); } catch (Exception e2) { e2.printStackTrace(); } } } if(ret == 0){ return xmlDocument ; }else{ return null ; } }

 

返回到flex的对象需要有限制

public String selectAllList() { System.out.println("start"); // TODO Auto-generated method stub List<Users_rt> list = this.users_rtMapper.selectAllList() ; System.out.println("list.size():/t"+list.size()); String filePath = "D://xx.xml"; // System.out.println("String/t"+filePath); Document xmlDocument = Users_rtServiceImpl.createXMLUsersDocument(filePath, list); // System.out.println(xmlDocument.toString()); if(xmlDocument != null){ System.out.println("创建成功"); }else{ System.out.println("创建失败"); } return XmlDeal.docToString(xmlDocument); }

 

注意:这里是返回的String,而不是xmldocument对象,

 

 

我的flex的代码如下:

 

<fx:Script> <!--[CDATA[ import flexunit.utils.ArrayList; import mx.collections.ArrayCollection; import mx.rpc.events.ResultEvent; [Bindable] private var xmllist:ArrayCollection = null; [Bindable] private var users :XML ; private function createUsersList():void{ users_rtService.selectAllList(); } //接收返回的xml文件 private function Handler_selectAllList(event:ResultEvent):void{ var xmldata:XML ; users = XML(event.result) ; //users_datagrid.dataProvider = users.children() ; } ]]--> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <mx:AMFChannel id="myamf" uri="http://localhost:8080/Hope/messagebroker/amf"/> <mx:ChannelSet id="channelSet" channels="{[myamf]}"/> <mx:RemoteObject destination="chartUsersServiceFlex" id="users_rtService" channelSet="{channelSet}" showBusyCursor="true" > <mx:method name="selectAllList" result="Handler_selectAllList(event)" /> </mx:RemoteObject> </fx:Declarations> <s:Panel x="159" y="125" width="757" height="234" title="Users表格"> <mx:DataGrid x="0" y="0" id="users_datagrid" width="754" height="153" dataProvider="{users.children()}" contentBackgroundColor="#E2BFBF" editable="false" rowCount="10"> <mx:columns> <mx:DataGridColumn headerText="ID" dataField="id"/> <mx:DataGridColumn headerText="用户名" dataField="username"/> <mx:DataGridColumn headerText="密码" dataField="password"/> </mx:columns> </mx:DataGrid> </s:Panel>

 

你可能感兴趣的:(xml,exception,datagrid,String,Flex,null)