2017-01-02 94 views
-2

http://idragonlk.com/channels.xml如何使用PHP在此xml中讀取此屬性?

這是xml。

想要在循環中讀取此通道和程序。

<?php 
$xmlDoc = new DOMDocument(); 
$xmlDoc->load("channels.xml"); 

$xmlObject = $xmlDoc->getElementsByTagName('channel'); 
$itemCount = $xmlObject->length; 

for ($i=0; $i < $itemCount; $i++){ 
    $title = $xmlObject->item($i)->getElementsByTagName('display-name')->item(0)->childNodes->item(0)->nodeValue; 
    print "Finished Item $title n<br/>"; 
} 
?> 

我試過這個。但它會打印txt值。我無法獲取屬性值。

想要得到

信道ID = '顯示名稱

圖標SRC = '的 '

郎='' 的值' value

編程起始= '' 值

program stop ='value

program channel =''v Alue

in program rating value

Plz誰能幫幫我嗎?

+0

回答了這個問題儘可能短。嘗試使用下面的代碼 –

回答

0

試試這個,對你有幫助。

<?php 

/*CURL PHP library to get file contents*/ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://idragonlk.com/channels.xml"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 
curl_close($ch); 
/*End*/ 

/*Takes a well-formed XML string and returns it as an object.*/ 
$XMLOutPut = simplexml_load_string($output); 


//$channel_list = getChannel_list($XMLOutPut); 
//$programme_list = getProgramme_list($XMLOutPut); 

echo "<pre>";print_r(getChannel_list($XMLOutPut)); 
echo "<pre>";print_r(getProgramme_list($XMLOutPut)); 
exit; 

/*Function to fetch channel and programme*/ 

//channel 
function getChannel_list($mainObj){$Result_List = array(); 
    for($i=0;$i<count($mainObj->channel);$i++){ 
     $Result_List[] = objectToArray($mainObj->channel[$i]); 
    } 
    return $Result_List; 
} 

//programme 
function getProgramme_list($mainObj){$Result_List = array(); 
    for($i=0;$i<count($mainObj->programme);$i++){ 
     $Result_List[] = objectToArray($mainObj->programme[$i]); 
    } 
    return $Result_List; 
} 

//Convert Object to array. 
function objectToArray($obj) { 
    if (is_object($obj)) {$obj = get_object_vars($obj);} 

    if (is_array($obj)) {return array_map(__FUNCTION__, $obj);} 
     else {return $obj;} 
} 
?> 

輸出

Array 
(
    [0] => Array 
     (
      [@attributes] => Array 
       (
        [id] => 4seven UK 
       ) 

      [display-name] => 4seven UK 
      [icon] => Array 
       (
        [@attributes] => Array 
         (
          [src] => 4seven UK.png 
         ) 

       ) 

      [url] => http://www.tvguide.co.uk 
     ) 

    [1] => Array 
     (
      [@attributes] => Array 
       (
        [id] => 5STAR UK 
       ) 

      [display-name] => 5STAR UK 
      [icon] => Array 
      ...... 
      ......