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 ();}}}