views:

760

answers:

2

Hi,

I am trying to generate a dynamic xml document in CakePHP to output to the browser.

Here is my controller code:

Configure::write ('debug', 0);
$this->layout = null;
header('Content-type: text/xml');
echo "<?xml version=\"1.0\"?>";

View is something like this:

<abc>
     something
</abc>

The output is probably as expected:

<?xml version="1.0"?><abc>something</abc>

The only problem is that there is a space before <?xml giving me an error:

XML Parsing Error: XML or text declaration not at start of entity
Line Number 1, Column 2:
 <?xml version="1.0"?><abc> something </abc>
-^

I know this problem in PHP, when you have php-start and end tags it leaves a space and creates problems, so, I tried to move the line echo "<?xml ver... to controller from the view to avoid that but it didn't help.

Thanks in advance. -happyhardik

+2  A: 

Yes, the problem should be an space after the php end tag somewhere.

As the php end tag is not mandatory, remove any end tag in all your models (if there're any), the controller you're asking about, from app_controller.php and app_model.php and from your view helpers... It should be somewhere but it is not easy to find

EDIT: In fact it could be also an space before the php begin tag, look into those files and check that the begin tag is at the absolute beginning of the file

EDIT AGAIN: There are people that have created some scripts for doing that automatically for you, take a look to:

http://ragrawal.wordpress.com/2007/11/07/script-for-removing-blank-spaces-before-and-after-php-tags/

rossoft
it **is** a space before the PHP begin tag, unless output buffering is enabled.
Kevin Peno
A: 

Actually, I find that it is most often a space AFTER the closing ?> tag in the layout file.

Also you should know that if you use the RequestHandler component and Router::parseExtensions( 'xml' ) in your routes.php you will automatically get the XmlHelper for use in your xml views.

The XmlHelper has a few neat functions in it. Check it out.

<?php
    echo( $xml->header( ));
    // outputs <?xml version="1.0" encoding="UTF-8" ?>
?>

The links for RequestHandler Component and the XmlHelper

http://book.cakephp.org/view/174/Request-Handling

http://book.cakephp.org/view/380/XML

Abba Bryant
In this case it does not seem it was in layout, as there's the line: $this->layout = null;So it is not using any layout
rossoft