Hi All,

I'm totally new to xml and I have to generate an XML file with the output of a database.

the structure isn't particularly complex (not many nested elements) so my question is:
can i use just a sequence of loops and echos like

echo "<tag> $variable </tag>";

etc.

Now, i've seen around very complex(to me!) functions or classes such as XMLWrite - Should I use one of those, or i can just go on with the loop+echo one?

    You can certainly just echo out the XML just like you could HTML in your script. You could also use something like the [man]DOM[/man] extension if you wanted, or some 3rd-party script if you find one you like. No one of those solutions is necessarily "better", though as things get more complex I might tend to opt for something like the DOM functions instead of writing lots of XML tags. But if you just want to echo out database query result rows without a lot of nesting or other XML complexity, you could just do something like:

    <?php
    header('Content-Type: text/xml');
    echo "<?xml version='1.0' encoding='utf-8'?>\n";
    echo "<data>\n";
    /* do DB connection stuff and define SQL, then... */
    $result = mysql_query($sql);
    if($result)
    {
       if(mysql_num_rows($result))
       {
          while($row = mysql_fetch_assoc($result))
          {
             echo "  <record>\n";
             foreach($row as $tag => $value)
             {
                echo "    <$tag>".htmlspecialchars($value)."</$tag>\n";
             }
             echo "  </record>\n";
          }
       }
       else
       {
          echo "  <error>No matches found</error>\n";
       }
    }
    else
    {
       echo "  <error>Error processing query</error>\n";
       error_log(mysql_error()."\n$sql");
    }
    echo "<data>";
    

      Thanks NogDog.

      If I wanted to create a file, instead of just displaying that on screen, could I just change the echo commands into a variable (eg, $output .= "<tag> htmlspecialchars($variable) </tag>"

      and then write everything to a file.xml using fwrite?

      Or writing to a file requires a different approach?

        Nothing special, other than the header() command becomes unnecessary since you won't be outputting the data via HTTP. You could write it all to a variable then write that variable to a file via [man]file_put_contents/man, or you could open up a file handle with [man]fopen/man and output the text line by line via [man]fwrite/man.

          cool, thanks.

          In the meantime I've tried with the other one, wrote everything and saved as create_XML.php.

          But when I open it with firefox, I get this error:

          XML Parsing Error: no element found
          Location: http://localhost/create_XML.php
          Line Number 94, Column 7:<data>
          ------^

          the output file seems to be correct, but firefox doesn't recognize it as xml.
          Maybe there is another closure tag or command that I'm missing? Eg, something to say that the XML file is finished?

            Found the reason... there was a missing / in the last tag

              If you copied-and-pasted my code, it looks like I forgot the slash on the final tag, which should be </data>.

                Write a Reply...