Flex [original] conversion/reading/writing of local XML files between XML and objects

Source: Internet
Author: User

Recently, many XML files have been operated, so the XML file operation class is encapsulated.

Main functions include converting 1.xml to object

2. Convert object to XML

3. read local XML (air)

4. write XML to local (air)

CodeAs follows:

 

 Package  {  Import  Flash. Events. eventdispatcher;  Import  Flash. filesystem. file;  Import  Flash. filesystem. filemode;  Import Flash. filesystem. filestream;  Import  Flash. system. system;  Import  Flash. xml. xmldocument;  Import  Flash. xml. xmlnode;  Import  MX. Collections. arraycollection;  Import  MX. rpc. xml. simplexmldecoder;  Import  MX. rpc. xml. simplexmlencoder;  Import Org. binyy. supermedia. App. utils. config. config;  /**  * XML manager *  */      Public   Class Xmlmanager Extends  Eventdispatcher {  Private   Static   Const Xmlhead: String = '<? XML version = "1.0" encoding = "UTF-8"?> ' ;  Public Function xmlmanager (){  Throw   New Error ("Don't init static class" );}  /**  * Update XML data *  @ Param  Data *  @ Param  Path *  */          Public   Static Function updatexmlitem (data: arraycollection, path: string ):Void  {Var XML: XML = Objecttoxml (util. sortitemindex (data). Source); writexmlfile (XML, PATH );  If  (XML) system. disposexml (XML );  If  (Data) data = Null  ;}  /**  * Remove XML data *  @ Param Data *  @ Param  Path *  */          Public   Static Function removexmlitem (data: arraycollection, path: string ): Void  {  For (Var I: Int = Data. Length-1; I> = 0; I -- ){  If (Data. getitemat (I). Selected) {data. removeitemat (I) ;}} var XML: XML = Objecttoxml (util. sortitemindex (data). Source); writexmlfile (XML, PATH );  If  (XML) system. disposexml (XML );  If  (Data) data = Null  ;}  /**  * Object to XML *  @ Param OBJ **  @ Return  XML *  */          Public   Static  Function objecttoxml (OBJ: Object): XML {var xmldocument: xmldocument = New  Xmldocument (); var xmlnode: xmlnode = ( New Simplexmlencoder (xmldocument). encodevalue (OBJ, New QNAME ("root"), Xmldocument); var XML: XML = New  XML (xmldocument. tostring ());  Return  XML ;}  /**  * XML to object *  @ Param  XML **  @ Return  Object *  */          Public  Static  Function xmltoobject (XML: XML): object {var xmldocument: xmldocument = New  Xmldocument (XML); var resultobj: Object = ( New  Simplexmldecoder (). decodexml (xmldocument );  Return  Resultobj ;}  /**  * Read local XML file *  @ Param  Path ** @ Return  XML *  */          Public   Static  Function readxmlfile (Path: string): XML {var file: File = File. applicationdirectory. resolvepath (PATH );  //  Trace ("read:" + file. nativepath ); VaR filestream: filestream = New  Filestream (); filestream. Open (file, filemode. Read); var XML: XML =XML (filestream. readutfbytes (filestream. bytesavailable); filestream. Close ();  Return  XML ;}  /**  * Write local XML file *  @ Param  XML file *  @ Param  Path *  */          Public   Static Function writexmlfile (XML: XML, path: string ): Void {Var file: File = New  File (file. applicationdirectory. resolvepath (PATH). nativepath );  //  Trace ("write:" + file. nativepath ); VaR filestream: filestream = New  Filestream (); filestream. Open (file, filemode. Write); var outputstring: String = Xmlhead; outputstring + = XML. toxmlstring (); filestream. writeutfbytes (outputstring); filestream. Close ();}}} 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.