php simplxml - 尝试获取非对象的属性

时间:2015-01-02 21:13:57

标签: php xml simplexml

因此,尝试使用代码访问XML文件中的播放器名称时出错。我不确定我是否正确访问了这些信息是否存在错误?

<?php
$xml = simplexml_load_file('trueskill.bcn');

$playername = $xml->SK92->Players[0]->Name;

echo $playername;
?>

使用XML文件

<?xml version="1.0" encoding="utf-8"?>
<SK92>
  <Settings Multiplier="200" Decay="0" />
  <Players>
    <Player Name="Mark" Team="" Invisible="false" Characters="" Alts="JFG;Mark;SEBA" />
    <Player Name="Antonio" Team="" Invisible="false" Characters="" Alts="Proxy;Toni" />
  </Players>
</SK92>

1 个答案:

答案 0 :(得分:1)

echo $xml->Players->Player['Name'];

应该这样做。

将它放在变量中:

$playername = $xml->Players->Player['Name'];
echo $playername;

旁注:

我想你想要访问所有玩家,你可以通过以下方式实现:

foreach ($xml->Players->Player as $play) {
    echo $play['Name'] . "<br>\n";
}