XML链接到PHP数组

时间:2014-10-03 16:46:22

标签: php arrays xml

我无法将此页面(http://ventrilostatus.net/xml/V98.DARKSTARLLC.COM:3789/)添加到php中。我对xml不是很好,我目前只能显示它: 服务器 渠道 渠道 渠道 (等)

有人可以帮助我显示频道名称和用户吗? 如果你看到它时没有客户,那将是:

<client admin="1" cid="994" phan="0" ping="186" sec="56935" name="WHERE USER'S NAME HIS HERE" comm=""/>

2 个答案:

答案 0 :(得分:0)

你可以使用PHP的simple_xml函数将xml文件加载到类中。

http://php.net/manual/de/book.simplexml.php

使用示例进行编辑:

// XML example
$xmlContent = '<server name="GuiltyofDestruction" phonetic="God" comment="http://guiltyofdestruction.com" auth="1" maxclients="25" uptime="137600" platform="Linux-i386" version="3.0.6" channelcount="36" clientcount="1" voicecodec="GSM 6.10" voiceformat="44 KHz, 16 bit"><channel cid="0" pid="-1" prot="0" name="Lobby" comm="">
    <channel cid="1033" pid="0" prot="1" name="Admin" comm=""/>
        <channel cid="1030" pid="0" prot="0" name="AFK Channel" comm="AFK">
            <client admin="1" cid="1030" phan="0" ping="182" sec="67575" name="US_ChairForce" comm=""/>
        </channel>
    </channel>
</server>';

// Loading XML information as object

//$xml = simplexml_load_file('http://ventrilostatus.net/xml/V98.DARKSTARLLC.COM:3789/'); // for files
$xml = simplexml_load_string($xmlContent); // for strings
foreach($xml->channel->channel as $channel) {
    if($channel->client)
        echo $channel->client->attributes()->name; // returns "US_ChairForce"
}

我认为现在很清楚。您必须遍历每个频道并寻找客户。

答案 1 :(得分:0)

抱歉这是我更新的代码:

<?PHP
echo "<table width=\"209\" bgcolor=\"#0d0d0d\">";
$xml = simplexml_load_file('http://ventrilostatus.net/xml/V98.DARKSTARLLC.COM:3789/'); // for files
//$xml = simplexml_load_string($xmlContent); // for strings

foreach($xml->channel as $lobby) {
echo "<tr><td>";
echo "<img src='http://view.light-speed.com//images/s_vent2/server_icon.gif' />";
echo "<font color=\"#FFFFFF\">";
echo $lobby->attributes()->name;
echo "</font>";
echo "</td></tr>";
}

foreach($xml->channel->channel as $channel) {
if($channel->attributes()->prot == "0"){
echo "<tr><td>&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_0_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $channel->attributes()->name;
echo "</font>";
echo "</td></tr>";    
                                        }else{
echo "<tr><td>&nbsp;&nbsp;";                                            
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_1_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $channel->attributes()->name;
echo "</font>";
echo "</td></tr>";                                          
  }

if($channel->client){
for ( $i = 0; $i < count($channel->client); $i++ ){
$client = $channel->client[ $i ];

        if ( $client->attributes()->cid != $client->attributes()->cid )

            continue;
echo "<tr><td>&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/player.gif' /><font color=\"#009e1a\">";
if($client->attributes()->admin == "1"){ 

     echo $client->attributes()->name; 
     echo "&nbsp;<font color=\"#ff0000\">(Admin)</font>";
     }else{
     echo $client->attributes()->name;
     }

echo "</td></tr>";
}
}

foreach($channel->channel as $subchannel) {
if($subchannel->attributes()->prot == "0"){
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_0_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $subchannel->attributes()->name;
echo "</font>";
echo "</td></tr>";    
                                        }else{
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;";                                            
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_1_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $subchannel->attributes()->name;
echo "</font>";
echo "</td></tr>";                                          
  }

if($subchannel->client){
for ( $i = 0; $i < count($subchannel->client); $i++ ){
$client = $subchannel->client[ $i ];

        if ( $client->attributes()->cid != $client->attributes()->cid )

            continue;
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/player.gif' /><font color=\"#009e1a\">";
if($client->attributes()->admin == "1"){ 

     echo $client->attributes()->name; 
     echo "&nbsp;<font color=\"#ff0000\">(Admin)</font>";
     }else{
     echo $client->attributes()->name;
     }

echo "</td></tr>";
}
}

foreach($subchannel->channel as $subchannel2) {
if($subchannel2->attributes()->prot == "0"){
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_0_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $subchannel2->attributes()->name;
echo "</font>";
echo "</td></tr>";    
                                        }else{
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                                            
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_1_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $subchannel2->attributes()->name;
echo "</font>";
echo "</td></tr>";                                          
  }

if($subchannel2->client){
for ( $i = 0; $i < count($subchannel2->client); $i++ ){
$client = $subchannel2->client[ $i ];

        if ( $client->attributes()->cid != $client->attributes()->cid )

            continue;
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/player.gif' /><font color=\"#009e1a\">";
if($client->attributes()->admin == "1"){ 

     echo $client->attributes()->name; 
     echo "&nbsp;<font color=\"#ff0000\">(Admin)</font>";
     }else{
     echo $client->attributes()->name;
     }

echo "</td></tr>";
}
}

foreach($subchannel2->channel as $subchannel3) {
if($subchannel3->attributes()->prot == "0"){
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_0_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $subchannel3->attributes()->name;
echo "</font>";
echo "</td></tr>";    
                                        }else{
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                                            
echo "<img src='http://view.light-speed.com//images/s_vent2/channel_1_empty.gif' />";
echo "<font color=\"#ffffff\">";
echo $subchannel3->attributes()->name;
echo "</font>";
echo "</td></tr>";                                          
  }

if($subchannel3->client){
for ( $i = 0; $i < count($subchannel3->client); $i++ ){
$client = $subchannel3->client[ $i ];

        if ( $client->attributes()->cid != $client->attributes()->cid )

            continue;
echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<img src='http://view.light-speed.com//images/s_vent2/player.gif' /><font color=\"#009e1a\">";
if($client->attributes()->admin == "1"){ 

     echo $client->attributes()->name; 
     echo "&nbsp;<font color=\"#ff0000\">(Admin)</font>";
     }else{
     echo $client->attributes()->name;
     }

echo "</td></tr>";
}
}
}

}
}
}
echo "</table>";
?>

显示

  • 大厅
  • 信道
  • 子信道
  • 子信道