2016-03-06 110 views
0

我對XML沒什麼問題。我的XML文件是這個樣子:使用PHP解析XML

http://pastebin.com/MQUB2W2B我的用戶,因爲引擎收錄本網站不支持XML代碼..://

,所以我需要選擇源支架「/流」源安裝「/」。我的PHP代碼如下所示:

Class stream { 

    Public Function __construct($host, $path, $user, $pass, $mpoint){ 

     $this->host = $host; 
     $this->path = $path; 
     $this->user = $user; 
     $this->password = $pass; 
     $this->mpoint = $mpoint; 

     $this->url = "http://{$this->user}:{$this->password}@{$this->host}/{$this->path}"; 
     $this->xml = simplexml_load_file($this->url); 

    } 

    Public Function audio($url, $format){ 

     return print "<audio controls> <source src=\"{$url}\" type=\"audio/{$format}\"> </audio>"; 

    } 

    Public Function stream_meta(){ 

     $xml = $this->xml->registerXPathNamespace('test', '/{$this->mpoint}'); 
     $result = $xml->xpath('test:artist'); 

     return $result; 

    } 

    Public Function nowplay(){ 

     return $this->xml->source[0]->artist; 
    } 

    Public Function download($format){ 

     $link = "http://{$this->host}/{$this->mpoint}"; 

     if($format == "m3u"){ 
      return "{$link}.m3u"; 
     } 

     elseif($format == "xspf"){ 
      return "{$link}.xspf"; 
     } 

     elseif($format == "vclt"){ 
      return "{$link}.vclt"; 
     } 

     return false; 

    } 


} 

所以現在我問我做錯了什麼或者我能做得更好?我需要選擇藝術家數據並且知道什麼是源碼「」這是什麼?「。我真的需要幫助。我不知道我能做什麼,我不發明其他方式來做到這一點。我很累,我真的需要幫助!請幫助我嗎?我不想使用「nowplay()」功能。因爲源安裝「/ example」更改有時...

回答

0

我不完全清楚你想要實現什麼,但這裏是獲取每個源的裝載和藝術家在XML中的基本示例:

function sources(){ 
    foreach($this->xml->source as $source) { 
     echo "Mount: " . $source->attributes()['mount'] . '<br>'; 
     echo "Artist: " . $source->artist . '<br>'; 
    } 
} 

還是要找到一個源藝術家:

$source = $this->find_source('/stream'); 
$artist = $source->artist; 

function find_source($s){ 
    foreach($this->xml->source as $source) { 
     if ($source->attributes()['mount'] == $s) { 
      return $source; 
     } 
    } 
} 

或者,你可以做XPath同樣無需使用一個循環。例如:

function find_source($s){ 
    return $this->xml->xpath('/icestats/source[@mount="'.$s.'"]')[0]; 
} 
+0

Thanks!但我怎麼才能得到藝術家?不是全部安裝 – T0niiiiii

+0

非常感謝!我必須去測試這個! – T0niiiiii

+0

就像只有一個藝術家的'source',或者'mount =「/ stream」'的藝術家一樣? –