2012-07-07 125 views
1

当我尝试创建SimpleXMLElement的新实例时,我不断收到此错误。“字符串不能被解析为XML”php错误

我手动检查了我的xml语法和在线工具,甚至复制/粘贴了php.net中的示例XML文件,但我仍然收到错误消息。

我的代码:

include 'example.php'; 
$namevalues= new SimpleXMLElement($xmlstr); 

的使用example.php:

<?php 
$xmlstr = <<<XML 
<?xml version='1.0' standalone='yes'?> 
<movies> 
<movie> 
    <title>PHP: Behind the Parser</title> 
    <characters> 
    <character> 
    <name>Ms. Coder</name> 
    <actor>Onlivia Actora</actor> 
    </character> 
    <character> 
    <name>Mr. Coder</name> 
    <actor>El Act&#211;r</actor> 
    </character> 
    </characters> 
    <plot> 
    So, this language. It's like, a programming language. Or is it a 
    scripting language? All is revealed in this thrilling horror spoof 
    of a documentary. 
    </plot> 
    <great-lines> 
    <line>PHP solves all my web problems</line> 
    </great-lines> 
    <rating type="thumbs">7</rating> 
    <rating type="stars">5</rating> 
</movie> 
</movies> 
XML; 
?> 

错误:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\AbyssRoot\htdocs\Forum\Sources\showhomework.php on line 266

+0

http://se.php.net/manual/en/simplexml.examples-errors.php – 2012-07-07 09:09:44

+0

测试上述代码中,正常工作中的“PHP 5.3.10-PL0-巴布亚(CLI) “ – 2012-07-07 09:14:59

回答

1

我通过确保复制此消息使用example.php无法找到。我认为这也是你的问题。

检查example.php是否在包含服务的目录中,或者放在同一个目录中。

如果确实如此,您需要调出错误报告级别,在异常之前我看到了一条通知和两条警告 - 这些确切地说明了导致此问题的原因,并且会为您节省大量时间,精力和头发。

Setting error reporting