0

我仍在尝试掌握 PHP 和 XML。我有一个要截断以节省空间的 XML。我想从每条记录中获取维度名称属性显示名称值。我还必须从每条记录中获取服务器管理器状态值。

我正在使用:

$completeurl = "http://website/data.xml";
$xml = simplexml_load_file($completeurl);
$xml->dimension;

for ($i = 0; $i < 2; $i++) {
    $servername = $dimensions[$i]->attributes()->display-name;
    $status = $dimensions[$i]->servermanager;
};

但是,我得到:

Fatal error: Call to a member function attributes() on a non-object in the lines I am trying to assign variables.

我正在删除我不想解析的维度之间的所有额外数据。截断的 XML 是:

<?xml version="1.0"?>
<aostats version="0.6" timestamp="2013-02-13 21:16:01 UTC">
    <dimension name="d1" display-name="Atlantean" loadmax="2" locked="0" players="100%">
        <servermanager status="1"/>
        <clientmanager status="0"/>
        <chatserver status="1"/>
    </dimension>
    <dimension name="d2" display-name="Rimor" loadmax="2" locked="0" players="100%">
        <servermanager status="1"/>
        <clientmanager status="0"/>
        <chatserver status="1"/>
    </dimension>
    <dimension name="dt" display-name="Test" loadmax="2" locked="0" players="100%">
        <servermanager status="1"/>
        <clientmanager status="0"/>
    </dimension>
</aostats>
4

1 回答 1

0

我自己通过实验找到了答案:

$completeurl = = "http://website/data.xml";
$xml = simplexml_load_file($completeurl);
$dimensions  = $xml->dimension;
for ($i = 0; $i < 3; $i++) {
$servername = $dimensions[$i]["display-name"];
$status = $dimensions[$i]->servermanager["status"];
}
于 2013-02-14T12:33:03.433 回答