无法使用 simplexml 和 PHP 加载 xml
由于某种原因,我似乎无法让 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Ó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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除
$string = "
之后的行,使其看起来像$string = "
Remove the line ending after
$string = "
so it looks like$string = "<?xml.....
我会像 PHP 手册中那样重新格式化你的字符串声明......
I would reformat your string declaration like in the PHP manual...
看起来这只是字符串变量开头的换行符问题。
如下更改。
Looks like it's merely an issue of the line break at the start of your string variable.
Change it as below.
你需要先设置 xml 进行测试,而且你还缺少一些卷曲的
you need to set the xml first the test, also you where missing some curly's