2010-05-24 55 views
6

我有一個simpleXml對象,並希望從該對象中讀取數據,我是PHP新手,不太瞭解如何執行此操作。對象細節如下。如何循環訪問PHP中的SimpleXML對象

我需要閱讀[說明]和[小時]。謝謝。

SimpleXMLElement Object (
    [@attributes] => Array (
     [type] => array 
    ) 
    [time-entry] => Array (
     [0] => SimpleXMLElement Object (
      [date] => 2010-01-26 
      [description] => TCDM1 data management: sort & upload 
      [hours] => 1.0 
      [id] => 21753865 
      [person-id] => 350501 
      [project-id] => 4287373 
      [todo-item-id] => SimpleXMLElement Object (
       [@attributes] => Array (
        [type] => integer 
        [nil] => true 
       ) 
      ) 
     ) 
     [1] => SimpleXMLElement Object (
      [date] => 2010-01-27 
      [description] => PDCH1: HTML 
      [hours] => 0.25 
      [id] => 21782012 
      [person-id] => 1828493 
      [project-id] => 4249185 
      [todo-item-id] => SimpleXMLElement Object (
       [@attributes] => Array (
        [type] => integer 
        [nil] => true 
       ) 
      ) 
) ) ) 

請幫助我。我嘗試了很多東西,但沒有得到正確的語法。

回答

10

這真的很難看了上面的,如果你可以給我們一個屏幕截圖或放棄行間距和縮進,這將是一個好一點 如果你有一個對象

$ XML是SimpleXML對象,您可以訪問像

$xml->element. 

看起來是時候進入一個元素的元素,在這種情況下,你會遍歷這樣的:

foreach($xml->{'time-entry'} as $time_entry) { 
    echo $time_entry->description . "<br />\n"; 
} 
+0

非常感謝,他們工作得很好。 你們搖滾, 再次感謝... – Aditya 2010-05-25 17:20:06

1

如果您在問題 -ed中的對象位於$element中,則以下內容將循環顯示兩個time-entry元素,同時打印出它們各自的descriptionhours值。

foreach ($element->{'time-entry'} as $entry) { 
    $description = (string) $entry->description; 
    $hours  = (string) $entry->hours; 
    printf("Description: %s\nHours: %s\n\n", $description, $hours); 
} 

輸出是這樣的:

Description: TCDM1 data management: sort & upload NFP SubProducers list 
Hours: 1.0 

Description: PDCH1: HTML 
Hours: 0.25 

注意,time-entry元素名稱必須被包裹在大括號作爲一個字符串否則$element->time-entry會嘗試讀取time元素和減去一個常數值爲entry,這顯然不是我們想要的。此外,descriptionhours值被鑄造爲到字符串(否則它們將是SimpleXMLElement類型的對象)。

1.請參閱variable variables 2.請參閱type casting

+0

非常感謝很多人,它工作得很好。你們搖滾,再次感謝.. – Aditya 2010-05-25 17:20:24

0

對不起,我是新來的PHP我剛開始學習,所以我的概念來自於C語言編程。我可能會在這裏做過多的工作/編碼......但我確信我會被某人大吼一聲並學習。

這將返回一個數組回其被很好的佈局,用於訪問數據的所有的各個位,而無需處理的SimpleXMLElement對象:

public $xmlFileInName = 'sample.xml'; 

public $errorMessage = "Failed to open file: "; 

public function actionGetArray() 
{ 
    try { 
     $xml = file_exists($this->xmlFileInName) ? 
     simplexml_load_file($this->xmlFileInName) : 
     exit($this->errorMessage. $this->xmlFileInName); 

     $xmlArray=$this->arrayFromSxe ($xml); 

     echo var_dump($xmlArray); 
    } 
    catch(Exception $e) { 
     echo $e->getMessage(); 
    } 
} 

public function arrayFromSxe($sxe) 
{ 
    $returnArray = array(); 
    foreach ((array)$sxe as $key=>$value) { 
     if(is_array($value) && !$this->is_assoc($value)) { 
      $indies = array(); 
      foreach($value as $secondkey=>$secondvalue) 
       $indies[$secondkey] = $this->arrayFromSxe($secondvalue); 
      $returnArray[$key] = $indies; 
     } 
     else { 
      if(is_object($value)) { 
       $returnArray[$key] = $this->arrayFromSxe($value); 
      } else { 
       $returnArray[$key] = $value; 
      } 
     } 
    } 
    return $returnArray; 
} 

function is_assoc($array) { 
    return (bool)count(array_filter(array_keys($array), 'is_string')); 
} 

注意,與多個XML標籤,即,在例如:

[time-entry] => Array (
    [0] => SimpleXMLElement Object (
    ... 
    ) 
    [1] => SimpleXMLElement Object (
    ... 
    ) 

爲了在你的代碼中解決這個問題,你必須知道關於XML文件結構的一些信息。 這將返回數組調用此行之後:

$xmlArray=$this->arrayFromSxe ($xml); 

這將有:

[time-entry][0] 
[time-entry][1] 

所以,你的代碼需要知道通過INT索引或以任何遍歷所有的時間條目它們是多次出現的xml元素的情況。

如果我所說的話沒有意義,那麼忽視它,只是遵守和評估代碼。

希望這會有所幫助。