1

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

4

3 回答 3

3

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/

于 2009-11-07T07:21:52.567 回答
0

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

于 2009-11-12T00:27:41.680 回答
0

Even though this does not answer the question directly. I thought it would be worth mentioning how easy it is to create dynamic XML views automatically using the CakePHP JSON and XML views helper, just in case people don't want to be doing it manually as seem to be the case above.

  • Step one: Add Router::parseExtensions(); to your routes.php file
  • Step two: Ensure the RequestHandler component is included in the relevant countroller by adding public $components = array('RequestHandler');
  • Step three: Now we only have to load some data and then display the data as XML or JSON automatically. Add something like the below:

    public function xml_view () {
       $this->set('data_array', $this->Model->find('all'));
       $this->set('_serialize', array('data_array'));       
    }
    

That's literally all we need to do to generate an XML or JSON respone for the xml_view action. Not even necessary to set up a view file. When your request is .../controller/xml_view.xml then CakePHP will return an XML document, and when .json is the extension, a JSON response will be generate. So easy I can't believe it!

于 2012-12-04T00:06:28.987 回答