XML parsing error: XML or text declaration not at the beginning of an object in php

Hi I am creating an xml file in php, but getting an error

XML parsing error: XML or text declaration not at the beginning of the object

My code is ---

<?php require_once('../../settings.php');
header("Content-type:text/xml");
$dom=new DOMDocument('1.0'); 
$dom->formatOutput = true;
$id=(int)$_GET['imageid'];
 $query="select * from tbl_image_gallery where imageId='$id' ORDER BY gallId DESC ";
$select=mysql_query($query);
$content = $dom->appendChild($dom->createElement('content'));
while($res=mysql_fetch_array($select))
{
    $image = $content->appendChild($dom->createElement('image'));
    $small_image_path = $image->appendChild($dom->createElement('small_image_path'));
    $small_image_path->appendChild($dom->createTextNode("image_gallery/load/images/small/".$res['image'])); 
    $big_image_path = $image->appendChild($dom->createElement('big_image_path'));
    $big_image_path->appendChild($dom->createTextNode("image_gallery/load/images/big/".$res['image']));
    $description = $image->appendChild($dom->createElement('description'));
    $description->appendChild($dom->createCDATASection($res['description']));
 }   
echo $test1 = $dom->saveXML(); 
?>

After searching on google, I found that this is a space problem before

I am deleting space but not getting the expected result.

+3
source share
2 answers

Clear the line below and try if the error message disappears. You probably have a space or something else output earlier in the file settings.php.

  header("Content-type:text/xml");

Check the settings.php file and remove the spaces, if any. Please note that if the settings.php file also includes some other files, you also need to check the location in this file.

+5
source

Use ob_clean();upecho $test1 = $dom->saveXML();

+4
source

All Articles