由于某些原因,我似乎无法获得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”。我在这里错过了一个重要的步骤吗?
谢谢!
答案 0 :(得分:4)
删除$string = "
后结尾的行,使其看起来像$string = "<?xml.....
答案 1 :(得分:2)
我会在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;
答案 2 :(得分:1)
看起来它只是字符串变量开头的换行问题。 将其更改如下。
$string = "
^^^^---- // error here
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
...
...";
$string = "<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
...
...";
答案 3 :(得分:0)
你需要首先将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);
}