4 Replies Latest reply on Nov 9, 2009 7:58 AM by Selva Vasanth

    How to Append and Update XML in flex

    Selva Vasanth

      I need a help… i am developing a desktop application using Flex & Air,

      I want to Append and update the xml using this application. How can i Append and Update the XML data???

      If the user give input it should be save in a XML . can anyone tell me the way to do this??


      Thanks in advance,

      Vasanth

        • 1. Re: How to Append and Update XML in flex
          Gregory Lafrance Level 6

          If this post answers your question or helps, please mark it as such.


          I'm creating an application that stores data in the "applicationStorageDirectory".     


          Mac storage directory is <appData>/<appId>/Local Store/
            where <appData> is user preferences folder, typically /Users/<user>/Library/Preferences


          Win: storage directory is <appData>\<appId>\Local Store\
            where <appData> is typically C:\Documents and Settings\<userName>\Application Data
            Note: Vista uses "junctions", which behave like shortcuts but look just like regular folders.
              It is not possible to access them, and any attempt to do so will give you an "Access Denied" error.
              Here is the real folder location:
                C:\Users\<userName>\AppData\Roaming\ContactManager\Local Store\contacts.xml


          Linux: <appData>/<appID>/Local Store/    where  <appData>  is  /home/<user>/.appdata


          Here is how to code to read from and write to a file there in an AIR app:


          var contactsFile:File;
          var contactsXML:XML;
          var contactsXLC:XMLListCollection;
          var stream:FileStream;

          contactsFile = File.applicationStorageDirectory;
          contactsFile = contactsFile.resolvePath("contacts.xml");

          stream = new FileStream();
          if (contactsFile.exists) {
              stream.open(contactsFile, FileMode.READ);
              contactsXML = XML(stream.readUTFBytes(stream.bytesAvailable));
              stream.close();
              contactsXLC = new XMLListCollection(contactsXML..contact);
          }else{
              var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n<contacts>\n';
              for each(var xml:XML in  contactsXLC){
                outputString += xml.toXMLString();       
                outputString += "\n";
              }
              outputString += "</contacts>\n";
              outputString = outputString.replace(/\n/g, File.lineEnding);
              stream = new FileStream();
              stream.open(contactsFile, FileMode.WRITE);
              stream.writeUTFBytes(outputString);
              stream.close();
          }

          1 person found this helpful
          • 2. Re: How to Append and Update XML in flex
            Selva Vasanth Level 1

            Thank You for your reply....


            Flash Player throwing this error. How can i fix this ??...


            "TypeError: Error #1009: Cannot access a property or method of a null object reference."


            Thanks in advance,

            Vasanth

            • 3. Re: How to Append and Update XML in flex
              Gregory Lafrance Level 6

              You may need to define some variables outside functions so they are "global".

               

              If this post answers your question or helps, please mark it as such.

              • 4. Re: How to Append and Update XML in flex
                Selva Vasanth Level 1

                Thank you again, i have declared that function, now the flash player is not throwing error.

                 

                But if i give inputs, its not getting saved on the XML, i don't know where those data's are being saved ...

                 

                 

                Kindly help me....

                 

                 

                Thanks in advance,

                 

                Vasanth