flex - Save data back to xml file using datagrid in flash builder -
i have xml file has employee details have registered company. populating xml using local server. have login page , have registration page. have populated xml data datagrid , can not update or add new employee through text input field. kindly me required process followed update xml file in hard drive.
<?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:registration="services.registration.*" creationcomplete="employeeservice.send()" currentstate="login" minwidth.registration="955" minheight.registration="600"> <s:layout> <s:basiclayout/> </s:layout> <fx:style source="assgpart2.css"/> <s:states> <s:state name="registration"/> <s:state name="login"/> <s:state name="home"/> </s:states> <fx:script> <![cdata[ import events.task; import mx.controls.alert; import mx.events.flexevent; import mx.events.validationresultevent; import mx.messaging.messages.errormessage; import mx.rpc.events.resultevent; import mx.utils.objectutil; private var indexcounter:number = 0; private function validateform():void { var vrename:validationresultevent= un.validate(); var vrepass:validationresultevent= ps.validate(); var vrecnfpass:validationresultevent= cp.validate(); var vrefname:validationresultevent= fn.validate(); var vrelname:validationresultevent= ln.validate(); var vreemail:validationresultevent= em.validate(); var vredate:validationresultevent= datevalid.validate(); if( vrename.type == validationresultevent.valid && vrepass.type == validationresultevent.valid && vrecnfpass.type == validationresultevent.valid && vrefname.type == validationresultevent.valid && vrelname.type == validationresultevent.valid && vreemail.type == validationresultevent.valid && vredate.type == validationresultevent.valid ) { if((male.selected== '0') && (female.selected == '0')) { mx.controls.alert.show("you forgot enter gender....!"); } else { mx.controls.alert.show("thank registaring us....!!!"); } } else { mx.controls.alert.show("enter valid details"); } } private function validatelogin(event:mouseevent):void { // getemployeesresult.token = login.getemployees(urname.text, passwd.text); } ]]> </fx:script> <fx:declarations> <s:httpservice id="employeeservice" url="c:\users\rknikhil\adobe flash builder 4.6\assignment1\src\assests\registration.xml" result="employeeservice_resulthandler(event)" method.registration="post"> </s:httpservice> <mx:stringvalidator id ="un" source =" {uname}" property = "text" /> <mx:stringvalidator id ="ps" source =" {pswd}" property = "text" /> <mx:stringvalidator id ="cp" source =" {cnfpass}" property = "text" /> <mx:stringvalidator id ="fn" source =" {fname}" property = "text" /> <mx:stringvalidator id ="ln" source =" {lname}" property = "text" /> <mx:stringvalidator source =" {email2}" property = "text" minlength.registration="8" tooshorterror.registration="min of 8 length" /> <mx:emailvalidator id ="em" source= "{email2}" property="text" requiredfielderror.registration="have contain ----@---.com" /> <mx:datevalidator id="datevalid" source = "{datev}" inputformat.registration="mm-dd-yyyy" property = "text" requiredfielderror.registration= "enter in mm-dd-yyyy format" /> <s:radiobuttongroup id="gender"/> <s:callresponder id="getemployeesresult"/> <registration:registration id="registration" fault="alert.show(event.fault.faultstring + '\n' + event.fault.faultdetail)" showbusycursor="true"/> <s:callresponder id="getemployeesresult2"/> <s:callresponder id="getemployeesresult3"/> <!-- place non-visual elements (e.g., services, value objects) here --> </fx:declarations> <s:panel id="login" includein="login " x="0" y="0" width="100%" height="100%" backgroundalpha="0.0" contentbackgroundalpha="0.0" title="login" backgroundalpha.login="400"> <s:label x="16" y="196" text="username"/> <s:textinput id="urname" x="87" y="186"/> <s:label x="12" y="239" text="password"/> <s:textinput id= "passwd" x="87" y="238"/> <s:button x="10" y="278" label="login" click="validatelogin(event)"/> <s:button x="568" y="278" label="register" click ="currentstate='registration'"/> <mx:datagrid id="datagrid" x="2" y="2" creationcomplete="datagrid_creationcompletehandler(event)" dataprovider="{getemployeesresult.lastresult}" editable.login="true"> <mx:columns> <mx:datagridcolumn datafield="username" headertext="username"/> <mx:datagridcolumn datafield="email" headertext="email"/> <mx:datagridcolumn datafield="firstname" headertext="firstname"/> <mx:datagridcolumn datafield="lastname" headertext="lastname"/> <mx:datagridcolumn datafield="dob" headertext="dob"/> <mx:datagridcolumn datafield="gender" headertext="gender"/> </mx:columns> </mx:datagrid> <s:label x="379" y="220" width="92" height="19" text="{employeeservice.lastresult.employees.employee.username}"/> </s:panel> <s:vgroup includein="registration" y="15" width="50%" height="50%" horizontalcenter="221"> <s:hgroup width="300" height="30"> <s:formitem label="username" required = "true"> <s:textinput id="uname" height="21" color="#d42222" text="{getemployeesresult.lastresult[0].username}"/> </s:formitem> </s:hgroup> <s:hgroup width="300" height="30"> <s:formitem label="password" required = "true"> <s:textinput id="pswd" width="150" color="#d42222" text="{getemployeesresult.lastresult[0].password}"/> </s:formitem> </s:hgroup> <s:hgroup width="300" height="30"> <s:formitem label="confirmpassword" required = "true" > <s:textinput id="cnfpass" width="150" color="#d42222" text="{getemployeesresult.lastresult[0].confirmpassword}"/> </s:formitem> </s:hgroup> <s:hgroup width="300" height="30"> <s:formitem label="email" required = "true" > <s:textinput id="email2" width="150" color="#d42222" text="{getemployeesresult.lastresult[0].email}" /> </s:formitem> </s:hgroup> <s:hgroup width="300" height="30"> <s:formitem label="first name" required = "true"> <s:textinput id="fname" width="150" color="#d42222" text="{getemployeesresult.lastresult[0].firstname}"/> </s:formitem> </s:hgroup> <s:hgroup width="300" height="30"> <s:formitem label="last name" required = "true"> <s:textinput id="lname" width="150" color="#d42222" text="{getemployeesresult.lastresult[0].lastname}"/> </s:formitem> </s:hgroup> <s:hgroup width="300" height="30"> <s:formitem label="date of birth" required = "true"> <s:hgroup> <s:textinput id="datev" width="150" color="#d42222" text="{getemployeesresult.lastresult[0].dob}"/> </s:hgroup> </s:formitem> </s:hgroup> <s:hgroup> <s:label id= "gen" backgroundcolor="#a6a1a1" chromecolor="#131212" color="#fcfaf8" paddingbottom="5" paddingleft="5" paddingright="5" paddingtop="5" text="gender"/> <s:radiobutton id="male" label="male" groupname="gender"/> <s:radiobutton id="female" label="female" groupname="gender" /> </s:hgroup> <s:button id="submit1" label="submit" /> <s:button id="back" label="back" click="currentstate='login'"/> </s:vgroup> <s:panel includein="home" x="4" y="2" width="100%" height="100%" fontfamily="times new roman" fontsize="20" fontweight="bold" title="welcome home page"> <s:bordercontainer x="0" y="0" width="100%" height="100%" fontfamily="times new roman" fontsize="12"> <s:form includein="home" x="404" y="25" width="100%" height="100%"> <s:formitem label="name"> <s:bordercontainer width="200" height="27"> <s:richtext width="100%" height="100%" color="#020202" fontsize="16" horizontalcenter="0" paddingbottom="5" paddingleft="5" paddingright="5" paddingtop="5" text="{urname.text} " verticalcenter="0" stylename.home="assgpart2.as"/> </s:bordercontainer> </s:formitem> <s:formitem label="comments"> <s:bordercontainer width="200" height="97"> <s:richeditabletext left="0" top="0" width="100%" height="100%" text=""/> </s:bordercontainer> </s:formitem> <s:button label="confirm" cornerradius="5"/> </s:form> <s:image includein="home" x="0" y="2" width="382" height="285" opaquebackground="true" smooth="true" source="images/lighthouse.jpg"/> </s:bordercontainer> </s:panel> <mx:linkbutton x="70" y="250" label="forgot password ?" focuscolor="#fd0101" rollovercolor="#fcf8f8" textdecoration="underline" x.login="85" y.login="311"/> </s:application>
ok, have copied out of app have use few years now, , runs on servers proven , works perfectly! of course totally own requirements adapted , these read write basics only. hope sort own application !!! rgards aktell
i used textinput components !!!
init .... import flash.filesystem.*; <mx:script> <![cdata[ import mx.rpc.events.faultevent; import mx.controls.alert; import mx.rpc.events.resultevent; import mx.rpc.xml.simplexmldecoder; import mx.rpc.http.httpservice; private var myhttp:httpservice = new httpservice(); private function initconfigcall():void { myhttp.url = "com/assets/data/.......? file.xml"; myhttp.send(); myhttp.resultformat = "xml"; myhttp.addeventlistener(resultevent.result, resulthandler); myhttp.addeventlistener(faultevent.fault, faulthandler); } private function resulthandler(evt:resultevent):void { var xmlstr:string = evt.result.tostring(); var xmldoc:xmldocument = new xmldocument(xmlstr); var decoder:simplexmldecoder = new simplexmldecoder(true); var resultobj:object = decoder.decodexml(xmldoc); // removed [0] on single node ! plnmsg.title = resultobj.application.configapp.appnewupdate +''+ resultobj.application.configapp.applastchanged; /* applastchanged.text = resultobj.application.configapp.applastchanged; appchangedsections.text = resultobj.application.configapp.appchangedsections; */ myhttp.disconnect(); myhttp = null; xmlstr = null; xmldoc = null; decoder = null; resultobj = null; } private function faulthandler(event:mx.rpc.events.faultevent):void { var faultinfo:string="fault details: "+event.fault.faultdetail+"\n\n"; faultinfo+="fault faultstring: "+event.fault.faultstring+"\n\n"; mx.controls.alert.show(faultinfo,"fault information"); var eventinfo:string="event target: "+event.target+"\n\n"; eventinfo+="event type: "+event.type+"\n\n"; mx.controls.alert.show(eventinfo,"event information"); myhttp.disconnect(); myhttp = null; myhttp.removeeventlistener(resultevent.result, resulthandler); myhttp.removeeventlistener(faultevent.fault, faulthandler); } ]]> </mx:script> <!-- --> <!-- *********************************************************************** --> <!-- xml file read out stream. --> <!-- *********************************************************************** --> <!-- --> <mx:script> <![cdata[ /* import flash.filesystem.filestream; import flash.filesystem.filemode; import flash.events.event; import flash.filesystem.*; */ private var myfilereadout:file; private var streamreadout:filestream = new filestream(); [bindable] private var xmldatareadout:xml; private function readoutxml():void { myfilereadout = file.desktopdirectory.resolvepath("files/"); myfilereadout = myfilereadout.resolvepath("dataone.xml"); if (myfilereadout.exists) { streamreadout.open(myfilereadout, filemode.read); xmldatareadout = xml(streamreadout.readutfbytes(streamreadout.bytesavailable)); txt000_0.text = xmldatareadout.search000_0; prefixtxt000_0.text = xmldatareadout.prefix000_0; countrytxt000_0.text = xmldatareadout.country000_0; ontxt000_0.htmltext = xmldatareadout.on000_0; streamreadout.close(); myfilereadout = null; streamreadout = null; xmldatareadout = null; } } ]]> </mx:script> private function savestartdata():void { xmldata = <urlgeneral/>; // xmldata.search000_0 = txt000_0.text; xmldata.prefix000_0 = prefixtxt000_0.text; xmldata.country000_0 = countrytxt000_0.text; xmldata.on000_0 = ontxt000_0.text; var outputstring:string = '<?xml version="1.0" encoding="utf-8"?>\n'; outputstring += xmldata.toxmlstring(); outputstring = outputstring.replace(/\n/g, file.lineending); stream = new filestream(); stream.open(myfile, filemode.write); stream.writeutfbytes(outputstring); stream.close(); // not use ! second save can give error. /* myfile = null; */ stream = null; xmldata = null; outputstring = null; system.disposexml(xmldata); } ]]> </mx:script> <mx:script> <![cdata[ /* import flash.filesystem.*; */ private var myfile:file; private var myfileone:file; private var stream:filestream; private var streamone:filestream; [bindable] private var xmldata:xml; [bindable] private var xmldataone:xml; public function appinit():void { myfile = file.desktopdirectory.resolvepath("files/"); myfile = myfile.resolvepath("dataone.xml"); // change on - (desktopdirectory). myfileone = file.applicationstoragedirectory.resolvepath("files/datafiles/"); myfileone = myfileone.resolvepath("....?.xml"); openxml(); openxmlone(); } private function openxml():void { stream = new filestream(); if (myfile.exists) { stream.open(myfile, filemode.read); xmldata = xml(stream.readutfbytes(stream.bytesavailable)); stream.close(); phraseonetxt.text = xmldata.phrase001_0; phrasetwotxt.text = xmldata.phrase002_0; phrasethreetxt.text = xmldata.phrase003_0; phrasefourtxt.text = xmldata.phrase004_0; phrasefivetxt.text = xmldata.phrase005_0; phrasesixtxt.text = xmldata.phrase006_0; phraseseventxt.text = xmldata.phrase007_0; phraseeighttxt.text = xmldata.phrase008_0; phraseninetxt.text = xmldata.phrase009_0; // not use ! second save can give error. /* myfile = null; */ stream = null; xmldata = null; system.disposexml(xmldata); } } private function lastrecordedrun():void { xmldataone = <urlgeneral/>; // base run 0. xmldataone.search000_0 = txt000_0.text; xmldataone.prefix000_0 = prefixtxt000_0.text; xmldataone.country000_0 = countrytxt000_0.text; xmldataone.infolastrun = lastruntxt.text; xmldataone.savestartdate = lastrundatetime.text; xmldataone.phrase001_0 = phraseonetxt.text; xmldataone.phrase002_0 = phrasetwotxt.text; xmldataone.phrase003_0 = phrasethreetxt.text; xmldataone.phrase004_0 = phrasefourtxt.text; xmldataone.phrase005_0 = phrasefivetxt.text; xmldataone.phrase006_0 = phrasesixtxt.text; xmldataone.phrase007_0 = phraseseventxt.text; xmldataone.phrase008_0 = phraseeighttxt.text; xmldataone.phrase009_0 = phraseninetxt.text; var outputstringone:string = '<?xml version="1.0" encoding="utf-8"?>\n'; outputstringone += xmldataone.toxmlstring(); outputstringone = outputstringone.replace(/\n/g, file.lineending); streamone = new filestream(); streamone.open(myfileone, filemode.write); streamone.writeutfbytes(outputstringone); streamone.close(); // not use ! save can give error. /* myfileone = null; */ streamone = null; xmldataone = null; outputstringone = null; system.disposexml(xmldataone); startgccycle(); }
Comments
Post a Comment