无法使用 simplexml 和 PHP 加载 xml

发布于 2024-11-11 02:11:53 字数 1100 浏览 1 评论 0原文

由于某种原因,我似乎无法让 PHP.net 提供的基本示例正常工作。这是我的代码:

    $string = "
<?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>
"; 

if(!$xml=simplexml_load_string($string)) 
    echo "failed to load xml";
else {

    print_r($xml);

}

所有这些打印都是“无法加载 xml”。我在这里错过了重要的一步吗?

谢谢!

For some reason I can't seem to get the basic example to work, provided by PHP.net. Here's my code:

    $string = "
<?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Ó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>
"; 

if(!$xml=simplexml_load_string($string)) 
    echo "failed to load xml";
else {

    print_r($xml);

}

All this prints is "failed to load xml". Am I missing an important step here?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

述情 2024-11-18 02:11:53

删除 $string = " 之后的行,使其看起来像 $string = "

Remove the line ending after $string = " so it looks like $string = "<?xml.....

许你一世情深 2024-11-18 02:11:53

我会像 PHP 手册中那样重新格式化你的字符串声明......

$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

I would reformat your string declaration like in the PHP manual...

$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;
好菇凉咱不稀罕他 2024-11-18 02:11:53

看起来这只是字符串变量开头的换行符问题。
如下更改。

$string = "
         ^^^^---- // error here
<?xml version='1.0' standalone='yes'?>
  <movies>
   <movie>
    ...
    ...";

$string = "<?xml version='1.0' standalone='yes'?>
  <movies>
   <movie>
    ...
    ...";

Looks like it's merely an issue of the line break at the start of your string variable.
Change it as below.

$string = "
         ^^^^---- // error here
<?xml version='1.0' standalone='yes'?>
  <movies>
   <movie>
    ...
    ...";

$string = "<?xml version='1.0' standalone='yes'?>
  <movies>
   <movie>
    ...
    ...";
鸢与 2024-11-18 02:11:53

你需要先设置 xml 进行测试,而且你还缺少一些卷曲的

$string = "<?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Ó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=simplexml_load_string($string);
if(!$xml){
    echo "failed to load xml";
}else {

    print_r($xml);

}

you need to set the xml first the test, also you where missing some curly's

$string = "<?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Ó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=simplexml_load_string($string);
if(!$xml){
    echo "failed to load xml";
}else {

    print_r($xml);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文