PHP Generated XML Problem
Posted: Thu May 24, 2007 3:15 am
I'm using some AJAX logic to pull information out of the database and display that content on the basies of what a user chooses from a drop-down menu.
The PHP and Javascript works fine apart from the PHP generates invlaid XML and when i check the source code i can see it has genertaed a valid xml file except there is some white space before the xml declartion and i am not sure why? Can anyone help or give me some advice please?
Main Page: http://www.warwickwebvisions.com/calls-abroad/
PHP Page: http://www.warwickwebvisions.com/calls- ... ntry=india
The Error i get is:
The PHP and Javascript works fine apart from the PHP generates invlaid XML and when i check the source code i can see it has genertaed a valid xml file except there is some white space before the xml declartion and i am not sure why? Can anyone help or give me some advice please?
Main Page: http://www.warwickwebvisions.com/calls-abroad/
PHP Page: http://www.warwickwebvisions.com/calls- ... ntry=india
The Error i get is:
This page contains the following errors:
error on line 1 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
Code: Select all
<?php
header('Content-Type: text/xml');
require_once('error_handler.php');
require_once('classes/database.class.php');
$country = $_GET['country'];
$table_id = 'country';
$connection = new database();
$link = $connection->database_connection();
$query = "select * from country where location ='$country'";
$results = mysql_query($query) or die(mysql_error());
if (!$results)
{
print 'There was a database error when executing';
print mysql_error();
exit;
}
//create new xml document
$doc = new DomDocument();
// create root node
$root = $doc->createElement('response');
$root = $doc->appendChild($root);
//process one row at a time
while($row = mysql_fetch_assoc($results)) {
//add node for each row
$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
//add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} //foreach
} //while
$xml_string = $doc->saveXML();
echo trim($xml_string);
?>