10 Replies Latest reply on Mar 4, 2009 2:53 PM by DwFAQ

    Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File

    Level 1
      Hello all,

      Here is a description of how I used ADDT's "Export Recordset as XML" behavior to write a real .xml file. ADDT's "Export Recordset as XML" behavior allows you to generate XML directly from a database using a user defined Recordset. Unfortunately, ADDT outputs the XML it generates directly to the browser. It does not just echo the data, it also writes headers before echoing the XML, so it's not just XML data that is sent.

      I am going to briefly describe my scenario for why I needed to do this so it will make more sense.

      We will be making a couple of simple edits to the ADDT include file: XMLExport.class.php

      This is located at: includes/XMLExport/XMLExport.class.php

      Please make a backup copy of this before making any changes to the file.

      The site I used this for is an architecture company. We created some flash pieces to display their projects, and these flash files read data out of XML files(such as image name, title, description, etc...). The XML data files are updated by the backend content management system so that the client can add, edit and delete projects as needed.

      Here we go. First use the ADDT Dynamic List Wizard and Dynamic Form Wizard to create your list, insert, update and delete functions. Then add an ADDT Custom Trigger to your Dynamic Form page set to execute AFTER. Make sure it is set to execute AFTER the Insert, Update and Delete Transactions in your Dynamic Form page. The function looks like this:

      function Trigger_GenerateXML(&$tNG) {

      ob_start(); // start buffer
      include ("outputXML.php");
      $output = ob_get_contents(); // assign buffer contents to variable
      ob_end_clean(); // end buffer and remove buffer contents

      }

      This code simply executes the outputXML.php file which we will create later. Take note of this code ob_start(), because it has many uses. Google: php ob_start() to learn more. For the purposes of this tutorial, this executes the php file outputXML.php and sets it to $output. We're not going to need $output for anything in this case, but it actually is a string variable that holds the XML code/data.

      Next, create a file called outputXML.php. Delete all html tags from this page...meaning do not have any html tags, head tags, meta tags, body tags, etc...we just want only php code here. After this add the appropriate "Recordset"(for the recordset you need) and then add the ADDT "Export Recordset as XML" Server Behavior.

      Then remove the require_once('../Connections/MyDB.php'); code from the top of the page. Because we call this php file from the previously created Dynamic Form page, the database connection has already been made.

      Next remove this line: mysql_select_db($database_MyDB, $MyDB);
      Again, we don't need this call because it is done in the Dynamic Form page which this script is called from.

      After that, look for this line: mysql_query($query_RecordsetProjects, $MyDB)
      $MyDB is a critical variable and we need to make it global in scope for this to work.

      So, where you deleted mysql_select_db($database_MyDB, $MyDB); replace with global $MyDB;

      The next step is to look for $xmlExportObj->Execute(); at the bottom of the outputXML.php page. This calls the function Execute() from the XMLExport.class.php which we will be modifying.

      Change: $xmlExportObj->Execute();

      To: $outputXML = $xmlExportObj->Execute();

      We are setting the $outputXML variable to hold the XML generated by Execute()

      Then add this code after the Execute()

      $myFile = "output.xml";
      $fh = fopen($myFile, 'w') or die("can't open file");
      fwrite($fh, $outputXML);
      fclose($fh);

      This will write the XML generated by ADDT to the file output.xml and each time the the Dynamic Form page is used to modify your database, the xml file will be regenerated.

      Now the final step is to edit includes/XMLExport/XMLExport.class.php and again, be sure to make a backup before editing. Don't be worried though...this is the easiest step :)

      Continued in post below..
        • 1. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
          Level 1
          Open includes/XMLExport/XMLExport.class.php and look around line 220 - 225.

          Change: $this->sendHeaders($size);

          To: //$this->sendHeaders($size);

          and

          Change: echo $document;

          To: return $document;

          That wasn't too bad right. Now anytime you use your Dynamic Form page to update your database, it will automatically write an updated XML file. One pitfall to look out for is correct read/write permissions on the the .xml file and containing folder. Also, your file paths need to be correct. This example should work fine if all your files are in one folder(Dynamic Form file, outputXML.php file, and output.xml file) and if the folder has correct read/write permissions set.

          Hope this helps. Please peer review and add corrections or suggestions if needed.

          Thanks,

          Shane
          • 2. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
            Günter Schenk Level 4
            Hey Shane,

            wow, this *is* absolutely cool -- what a great suggestion and analysis !!!

            -----
            Please peer review and add corrections or suggestions if needed
            -----

            well, the only downside of the suggested XMLExport.class.php modification is, that you can´t use this file for regular purposes anymore -- but this issue can be overcome by adding some additional logic to both the file XMLExport.class.php as well as the PHP file which contains the XMLExport behaviour.

            ------------------------------
            changes to XMLExport.class.php
            ------------------------------

            1. inside the class XMLExport, define a new "var":

            /**
            * The rendering type (echo to browser or write to a file) which will be set for the exported XML file.
            */

            var $xmlRendering;

            2. add a new line to the function XMLExport():

            $this->xmlRendering = '';

            ...and give it default blank value (''), which is meant to have the script use the default "echo to browser" behaviour unless specified otherwise

            3. add a new function:

            function setXMLRendering($xmlRendering) {
            $this->xmlRendering = strtoupper(trim($xmlRendering));
            }

            4. now let´s wrap your suggested modification inside an if/else condition:

            if ($this->xmlRendering == 'STATIC') {
            return $document;
            }
            else {
            $this->sendHeaders($size);
            echo $document;
            }

            ...to make sure that the script doesn´t break when used the regular way

            ----------------------------------
            changes to the XMLExport behaviour
            ----------------------------------

            now that´s basically very easy :: whenever you actually need to create a static file, you´d just have to add this line manually:

            $xmlExportObj->setXMLRendering("STATIC");

            According to the logic we added to XMLExport.class.php, this line is optional, and if this variable isn´t set respectively doesn´t have the (string-to-lower) value "STATIC" assigned, the XMLExport function will use the default blank value, which internally translates to "echo to browser".

            Anyone who´s reading this: please note that my suggestion is to be considered experimental, though I successfully applied similar stuff to other scripts in the past. Shane, could you please give this a try and tell us if it works ? :-)

            Cheers,
            Günter Schenk
            Adobe Community Expert, Dreamweaver
            • 3. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
              Level 1
              Günter,

              Thanks for the great suggestions. I added the code you provided and it works fine. I tested generating the XML both ways.

              Maybe they will use your version in the next ADDT release :) Seems it would be very simple to add this to ADDT and give an option in the wizard for how to output the XML.

              Thanks,

              Shane
              • 4. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                oicram Level 1
                No comments on this???
                I Do.

                WOW!!!! :) (great profound and intelligent comment, done).

                Actually, I'm trying to create my back-end using traditional mysql/php/ADDT/Günter Help/ approach. :)

                After this, I'm looking forward to have the Flash reading this XML generated data.

                This data on the front end will be "read-only" (so I don't need any fancy Flex on this), I just need the Flash to read the generated XML file.

                It's ok... if we have more then one update and more then one insert? Should we put the Costum trigger also, AFTER the delete SB?
                Where can we find more information regarding this topic: Where we could see more details about how to make this connection between ADDT and XML so that we can get that XML on a flash or spry based site?

                For example: This is a very nice feature. How to relate the ADDT "show image" with some kind of XML export so that image can be work, later on with spry or flash?

                Thanks a lot.
                • 5. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                  Level 1
                  Hi,
                  Could u to send your XMLExport.class.php, because i had make a changement but it's doesn't work !!!
                  Thx u very much

                  Phy from france
                  • 6. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                    Level 1
                    Hi All..

                    Thanks for sharing this with us Shane and Gunter.
                    May I also offer to the thread the Spry approach for PHP/MySQL here:
                    http://beta/spry1.6/samples/utils/query2xml.html
                    Select the Server Side Utilities tab, then click on querytoXML on the next page then check the PHP suggestions.

                    I find these methods very useful. Start in DW by creating a record set for the data you need then edit the file accordingly.
                    These are used by me for Spry data sets, Spry menus, and other bit'n'pieces like feeding XML to things like Slideshow Pro. Great for situations where you need several XML files on the same site. Furthermore, easily migrated from site to site.

                    Regards,
                    • 7. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                      MoCleveland Level 1
                      This is a great script. I'm trying to use the Gunter script.

                      Unfortunately, i'm having a little trouble with it. I have a really dumb question. Where exactly does the if/else statement go?

                      f ($this->xmlRendering == 'STATIC') {
                      return $document;
                      }
                      else {
                      $this->sendHeaders($size);
                      echo $document;
                      }

                      I placed it after the function setXMLRendering and I am getting a syntax error: unexpected T_IF, expecting T_FUNCTION in /var/www/includes/XMLExport/XMLExport.class.php

                      I thought I made all the other edits correctly.

                      Thanks...
                      • 8. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                        Günter Schenk Level 4
                        Hi Maurice,

                        -----
                        I placed it after the function setXMLRendering
                        -----

                        the last but one function in the file XMLExport.class.php is "Execute", and at the end of this function you´ll see the following two lines:

                        $this->sendHeaders($size);
                        echo $document;

                        These two lines will need to be replaced with the if/else statement you mention, that´s all ;-)

                        Cheers,
                        Günter Schenk
                        Adobe Community Expert, Dreamweaver
                        • 9. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                          MoCleveland Level 1
                          Ok, that makes perfect sense.

                          I can't believe I didn't see that.

                          Thanks, works great.
                          • 10. Re: Tutorial: How to Use ADDT Export Recordset as XML to Write Real XML File
                            DwFAQ Level 4
                            Heya,

                            Why not just user scripting to generate xml from database like this?

                            http://labs.adobe.com/technologies/spry/samples/utils/query2xml.html