1

我正在尝试使用简单的 xml 阅读器从该文件中读取 xml 节点的属性

<songs>
<song title="On Mercury" artist="Red Hot Chili Peppers" path="/red-hot-chili-peppers/on-mercury.mp3" />
<song title="Universally Speaking" artist="Red Hot Chili Peppers" path="/red-hot-chili-peppers/universally-speaking.mp3" />
</songs>

我用那个代码来阅读它,但它给了我 xml 解析错误

<?php
$xml = simplexml_load_file("playlist.xml") 
       or die("Error: Cannot create object");

foreach($xml->children() as $data){
      echo $data->song['title'];
      echo "<br />";

}

?>

请帮我

4

1 回答 1

0

您不需要同时调用->children()->song。第一个为您提供特定节点的所有子节点,而不考虑标签名称,第二个为您提供标签名称为“song”的特定节点的所有子节点。

尝试:

foreach($xml->song as $song){
    echo $song['title'];
    echo "<br />";
}

这相当于:

foreach($xml->children() as $data) {
    if ( $data->getName() == 'song' ) {
        echo $data['title'];
        echo "<br />";
    }
}
于 2012-09-17T16:06:37.647 回答