我正在尝试从该文件中读取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 />";
}
?>
请帮帮我
答案 0 :(得分: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 />";
}
}