7 Replies Latest reply on Jan 11, 2008 3:38 AM by ColinLoretz

    Save generated XML to an XML file

      The following code creates an XML object and the function runs a query from Salesforce and populates the XML object with the results.

      How would I go about saving this XML data to a file, say "data/devNotes.xml" ?

      private var myDevNotes:XML = <notes></notes>;

      private function loadDevNotes():void
      //Query code removed for brevity
      for (var j:int=0;j<qr.records.length;j++)
      var newNote:XML =
      <note sfid= {qr.records[j].Id}>
      myDevNotes = myDevNotes.appendChild(newNote);
        • 1. Re: Save generated XML to an XML file
          m_hartnett Level 3
          You need to write some server code that will take the xml from the request object and write it to the server.

          We use java and JDOM to write a file to the server.
          Flex needs to make an HTTPService call to some server object that will do the save.
          • 2. Re: Save generated XML to an XML file
            ColinLoretz Level 1

            Thank you for the quick response. It makes sense but I fear this might be over my head. It looks like I'll be doing a lot of experimentation with HTTPServices in the next couple days. This is the biggest obstacle in allowing me to finish an app I've been working on.
            • 3. Re: Save generated XML to an XML file
              dietmar.paulus Level 1
              if you want to save the xml-object to file you also can try to use sharedobjects (see the docs) but they are limited in size
              • 4. Re: Save generated XML to an XML file
                chris.huston.t10 Level 3
                Here is some sample code I use to save an XML file to the client computer via PHP:

                private var fileToDownload:FileReference;
                private function saveXML(xmlData:XML):void {
                var request:URLRequest = new URLRequest();
                request.url = " http://your.server.com/saveXML.php";
                request.method = URLRequestMethod.POST;
                var params:URLVariables = new URLVariables();
                params.xmlData = xmlData;
                request.data = params;
                fileToDownload = new FileReference();
                fileToDownload.download(request, "devNotes.xml");

                And on the PHP side, I have:

                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Cache-Control: no-store, no-cache, must-revalidate');
                header('Cache-Control: post-check=0, pre-check=0', FALSE);
                header('Pragma: no-cache');
                $xmlData = $_POST[xmlData];
                echo "$xmlData";

                I hope this helps.

                • 5. Re: Save generated XML to an XML file
                  ColinLoretz Level 1
                  Thank you very much for the code sample. That definitely helps.

                  I forgot to mention that this is an AIR app. Can't I access to the file system without requiring the user to choose a location to save the file to?

                  Finally, what I may be attempting to do may not be the best way of going about things. Essentially, the process I'm going for is:
                  1. Run a query against Salesforce
                  2. Parse query result into XML
                  3. Save to devNotes.xml
                  4. devNotes.xml is then used as a dataProvider source
                  • 6. Re: Save generated XML to an XML file
                    chris.huston.t10 Level 3
                    In AIR it is quite a bit easier to do what you want. Here is some sample code:

                    private function save_xmlData():void {
                    var xmlFile:File = File.applicationStorageDirectory; // or File.documentsDirectory or File.desktopDirectory
                    xmlFile = xmlFile.resolvePath('data/devNotes.xml"');
                    var outputString:String = myDevNotes.toXMLString();
                    outputString = outputString.replace(/\n/g, File.lineEnding);
                    var stream:FileStream = new FileStream();
                    stream.open(xmlFile, FileMode.WRITE);

                    • 7. Re: Save generated XML to an XML file
                      ColinLoretz Level 1

                      Thank you for all the code examples. You've been a wonderful help.