“String could not be parsed as XML” php error

2019-03-01 00:31发布

I keep getting this error when I try to create a new instance of SimpleXMLElement.

I checked my xml syntax manually and with online tools, and even copy/pasted the example XML file from php.net, but I’m still getting the error.

My code:

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

The 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;
?>

The error:

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

1条回答
可以哭但决不认输i
2楼-- · 2019-03-01 01:26

I replicated this message by making sure that example.php cannot be found. I think this is your problem too.

Check that example.php is in a directory from which includes are served or else, put it in the same directory.

If this is indeed the case, you need to turn up your error reporting levels, I saw a notice and 2 warnings prior to the exception - these spell out exactly what was causing this problem, and will save you a lot of time, effort and hair.

Setting error reporting

查看更多
登录 后发表回答