Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML链接到PHP数组_Php_Arrays_Xml - Fatal编程技术网

XML链接到PHP数组

XML链接到PHP数组,php,arrays,xml,Php,Arrays,Xml,我无法将此页面()数组放入php。我对xml不是很在行,目前只能让它显示: 服务器 频道 频道 频道 (等) 有人能帮我把频道名称和用户显示出来吗? 如果在查看时没有客户端,则会是: <client admin="1" cid="994" phan="0" ping="186" sec="56935" name="WHERE USER'S NAME HIS HERE" comm=""/> U可以使用PHP的简单xml函数将xml文件加载到类中 用一个例子编辑: // XML ex

我无法将此页面()数组放入php。我对xml不是很在行,目前只能让它显示: 服务器 频道 频道 频道 (等)

有人能帮我把频道名称和用户显示出来吗? 如果在查看时没有客户端,则会是:

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

U可以使用PHP的简单xml函数将xml文件加载到类中

用一个例子编辑:

// 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"
}
//XML示例
$xmlContent='1〕
';
//将XML信息作为对象加载
//$xml=simplexml\u加载\u文件('http://ventrilostatus.net/xml/V98.DARKSTARLLC.COM:3789/'); // 档案
$xml=simplexml_load_字符串($xmlContent);//弦乐
foreach($xml->channel->channel as$channel){
如果($channel->client)
echo$channel->client->attributes()->name;//返回“US_ChairForce”
}

我想现在清楚了。您必须遍历每个频道并查找客户端。

抱歉,这是我更新的代码:

<?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>";
?>

显示

  • 大厅
  • 渠道
  • 子通道
  • 子通道

您可以使用XML-PHP库将有效的XML代码转换为PHP对象或数组。正如我所说的,我不擅长XML,我以前确实尝试过阅读这篇文章。我的问题是我只能将其发送到post Server-channel-channel-channel-etc。因为xml不是服务器名称channel NAME,所以我不知道如何获取信息。您可以迭代每个子对象,并且可以使用$obj->attributes()->attribute访问标记的所有属性。如果你想拥有用户,用foreach循环遍历每个对象。别忘了勾选这个。;)