Does anyone know if there's a XML code generator in PHP?
I need to generate XML from DB using PHP. I want to see if there's already a code generator somewhere.
Thanks in advance for all the help.
Does anyone know if there's a XML code generator in PHP?
I need to generate XML from DB using PHP. I want to see if there's already a code generator somewhere.
Thanks in advance for all the help.
here is one that I use:
<?PHP
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=yourfilename.xls");
header("Pragma: no-cache");
header("Expires: 0");
// DEFINE DB ELEMENTS
require_once('yourdatabasescript.php');
$conn = db_connect();
if (!$conn)
return 'Could not connect to database server - please try later.';
// QUERY DB FOR SELECTED DATA
$select = "SELECT * FROM your_tbl";
$export = mysql_query($select);
$row_export = mysql_fetch_assoc($export);
$fields = mysql_num_fields($export);
$count = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
echo $header."\n".$data;
//IF STATMENT THAT IS PRODUCED IF NO DATA IS PRESENT
if ($data == "") {
$data = "\n(0) Records Found!n";
}
?>
What thorpe said: PHP doesn't care what the output is. All you have to do is start the page with
header('Content-type: text/xml');
or
header('Content-type: application/xml')
depending on whether the result is intended to be human-readable or not; and output XML in exactly the same way you'd output HTML.
I had to do this at the VERY top of my script before any includes or anything to get this to work right:
header( "Content-type: text/xml\n" );
echo '<?xml version="1.0"?>';
Otherwise I couldn't get the xml declaration in the output. Minor point if you don't actually need the <?xml version="1.0"?> line, however.
That would be because you've got PHP configured with the (deprecated) short_tags option turned on.
You can use the DOM functions to generate an XML document in memory (which is what a DOM is), then output it.
Be aware that the DOM functions do not check for things which are invalid when you're creating a document, so you can create badly formed documents - for example, using attributes with illegal names.
Pay special attention to getting the character encoding right with DOM. All functions expect UTF8 regardless of what encoding your app is using.
Mark