2014-03-27 121 views
1

調用的Prestashop web服務後,我收到響應,如下圖所示:如何從SimpleXMLElement對象檢索值

SimpleXMLElement Object 
(
    [order] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [id] => 1 
         ) 

       ) 

      [1] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [id] => 2 
         ) 

       ) 

     ) 

) 

我試圖通過循環以檢索內容,如下圖所示:

foreach($resources as $resource){ 
    echo '<pre>'; 
    print_r($resource['id']); 
    echo '</pre>'; 
} 

這給了我:

SimpleXMLElement Object 
(
    [0] => 1 
) 
SimpleXMLElement Object 
(
    [0] => 2 
) 

如何檢索1和2這些對象的值?感謝

回答

1

我討厭的SimpleXML ...

<?php 
$xml = file_get_contents('xml.xml'); 
$xml = new SimpleXMLElement($xml); 

foreach($xml as $key => $value) 
{ 
     $attrs = $value->attributes(); 
     foreach($attrs as $attr_k => $attr_v) 
       echo $attr_k.": ".$attr_v."\n"; 
} 
+0

如果可以,更換爲DOM – ZiTAL

+0

任何鏈接,我可以閱讀更多有關DOM更換呢? – Lomse

+0

http://www.binarytides.com/php-tutorial-parsing-html-with-domdocument/ – ZiTAL