Cakephp生成xml错误-空格
我正在尝试在 CakePHP 中生成动态 xml 文档以输出到浏览器。
这是我的控制器代码:
Configure::write ('debug', 0);
$this->layout = null;
header('Content-type: text/xml');
echo "<?xml version=\"1.0\"?>";
View 是这样的:
<abc>
something
</abc>
输出可能符合预期:
<?xml version="1.0"?><abc>something</abc>
唯一的问题是 之前有一个空格给我一个错误:
XML Parsing Error: XML or text declaration not at start of entity
Line Number 1, Column 2:
<?xml version="1.0"?><abc> something </abc>
-^
我知道这个问题PHP,当你有 php-start 和 end 标签时,它会留下一个空格并产生问题,所以,我尝试将行 echo " 从视图移动到控制器 但没有帮助。
避免这种情况, -快乐哈迪克
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,问题应该是 php 结束标记后面的空格。
由于 php 结束标记不是强制性的,因此请从 app_controller.php 和 app_model.php 以及视图助手中删除所有模型(如果有)、您要询问的控制器中的任何结束标记...应该在某个地方,但不容易找到
编辑:事实上,它也可能是 php begin 标记之前的空格,查看这些文件并检查 begin 标记是否位于 php begin 标记的绝对开头文件
再次编辑:有人创建了一些脚本来自动为您执行此操作,请查看:
http://ragrawal.wordpress.com/2007/11/07/script-for-删除 php-tags 之前和之后的空白/
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/
实际上,我发现它通常是结束后的一个空格?>布局文件中的标签。
此外,您还应该知道,如果您在routes.php 中使用RequestHandler 组件和Router::parseExtensions( 'xml' ),您将自动获得XmlHelper 以在您的xml 视图中使用。
XmlHelper 中有一些简洁的函数。一探究竟。
RequestHandler 组件和 XmlHelper 的链接
http://book.cakephp.org/view/ 174/请求处理
http://book.cakephp.org/view/380 /XML
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.
The links for RequestHandler Component and the XmlHelper
http://book.cakephp.org/view/174/Request-Handling
http://book.cakephp.org/view/380/XML
尽管这并没有直接回答问题。我认为值得一提的是,使用 CakePHP JSON 和 XML 视图帮助器自动创建动态 XML 视图是多么容易,以防万一人们不想像上面的情况那样手动执行此操作。
Router::parseExtensions();
添加到您的routes.php 文件中public $components = array('RequestHandler) 确保 RequestHandler 组件包含在相关的计数器中');
第三步:现在我们只需加载一些数据,然后自动将数据显示为 XML 或 JSON。添加如下内容:
这实际上就是我们为 xml_view 操作生成 XML 或 JSON 响应所需要做的全部事情。甚至不需要设置视图文件。当您的请求是 .../controller/xml_view.xml 时,CakePHP 将返回一个 XML 文档,当扩展名为 .json 时,将生成一个 JSON 响应。如此简单我简直不敢相信!
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.
Router::parseExtensions();
to your routes.php filepublic $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:
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!