Search code examples
phpsqlxmlpear

Saving XML_Query2XML output to file?


I have a short script that utilizes the XML_Query2XML PEAR package. It pulls data from a SQL database and outputs to the browser. The XML that appears in the browser is exactly what I want to be saved to a file, but any attempts to use ob_get_contents or any of the other methods I'm familiar with result in a blank output file. The code is as follows:

<?php


    set_include_path('/Library/WebServer/Documents/PEAR/');
    include 'XML/Query2XML.php';
    include 'MDB2.php';

    try {
        // initialize Query2XML object
        $q2x = XML_Query2XML::factory(MDB2::factory('mysql://root:[email protected]/site'));

        $sql = "SELECT * FROM Products";
        $xml = $q2x->getFlatXML($sql);



        header('Content-Type: text/xml');
        $xml->formatOutput = true;
        echo $xml->saveXML();
    } catch (Exception $e) {
        echo $e->getMessage();
    }


    ?>

I'm wondering what the general procedure is for saving files with this plugin and output type (XML). Any help is greatly appreciated.


Solution

  • The $xml variable is a DOMDocument object, which means you can use its methods to save it into a file, e.g. save:

    $xml->save('foo.xml');