2013-10-24 67 views
5

我想通過一個XML對象使用的SimpleXML在PHP循環XML對象 - 對象格式如下: -遍歷用SimpleXML

我收到以下錯誤,當我試圖通過獲得元素標籤名稱 - 任何人都可以解釋我做錯了什麼?

Call to undefined method SimpleXMLElement::getElementsByTagName() on ... 

下面是我從讀取XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<response> 
    <products> 
     <item> 
      <product_id>32417</product_id> 
      <manufacturer>Alcatel</manufacturer> 
      <model>Sparq 2</model> 
      <deeplink>http://www.mysite.com/sc_offer?gid=32417</deeplink> 
      <thumbnail_URL>http://www.mysite.com/images/devices/thumbs/Alcatel-Sparq-II.jpg</thumbnail_URL> 
      <image_URL>http://www.mysite.com/images/devices/Alcatel-Sparq-II.jpg</image_URL> 
      <price_not_working>0.00</price_not_working> 
      <price_poor>0.00</price_poor> 
      <price_fair>20.00</price_fair> 
      <price_good>25.00</price_good> 
      <price_perfect>25.00</price_perfect> 
      <price_new>25.00</price_new> 
      <battery_new>1.00</battery_new> 
      <battery_perfect>1.00</battery_perfect> 
      <battery_good>1.00</battery_good> 
      <battery_fair>1.00</battery_fair> 
      <battery_poor>0.00</battery_poor> 
      <charger_new>1.00</charger_new> 
      <charger_perfect>1.00</charger_perfect> 
      <charger_good>1.00</charger_good> 
      <charger_fair>1.00</charger_fair> 
      <charger_poor>0.00</charger_poor> 
      <packaging_new>1.00</packaging_new> 
      <packaging_perfect>1.00</packaging_perfect> 
      <packaging_good>1.00</packaging_good> 
      <packaging_fair>1.00</packaging_fair> 
      <packaging_poor>0.00</packaging_poor> 
     </item> 
     <item> 
      <product_id>31303</product_id> 
      <manufacturer>Apple</manufacturer> 
      <model>iPhone 3G 8gb</model> 
      <deeplink>http://www.mysite.com/sc_offer?gid=31303</deeplink> 
      <thumbnail_URL>http://www.mysite.com/images/devices/thumbs/iPhone 8 3G.jpg</thumbnail_URL> 
      <image_URL>http://www.mysite.com/images/devices/iPhone 8 3G.jpg</image_URL> 
      <price_not_working>0.00</price_not_working> 
      <price_poor>0.00</price_poor> 
      <price_fair>7.00</price_fair> 
      <price_good>2.00</price_good> 
      <price_perfect>2.00</price_perfect> 
      <price_new>2.00</price_new> 
      <battery_new>1.00</battery_new> 
      <battery_perfect>1.00</battery_perfect> 
      <battery_good>1.00</battery_good> 
      <battery_fair>1.00</battery_fair> 
      <battery_poor>0.00</battery_poor> 
      <charger_new>1.00</charger_new> 
      <charger_perfect>1.00</charger_perfect> 
      <charger_good>1.00</charger_good> 
      <charger_fair>1.00</charger_fair> 
      <charger_poor>0.00</charger_poor> 
      <packaging_new>1.00</packaging_new> 
      <packaging_perfect>1.00</packaging_perfect> 
      <packaging_good>1.00</packaging_good> 
      <packaging_fair>1.00</packaging_fair> 
      <packaging_poor>0.00</packaging_poor> 
     </item> 
    </products> 
</response> 

我的PHP代碼如下:

$devices = $xml->getElementsByTagName("response"); // error on this line 

我想使器件陣列,所以我可以使用數據在foreach循環中。

回答

22

SimpleXML沒有getElementsByTagName()方法(DOMDocument)。

在SimpleXML中,對象(例如$xml)被視爲根元素。所以,你可以循環通過產品項目,像這樣:

$devices = array(); 

$xml = simplexml_load_string($xmlString); 
foreach($xml->products->item as $item) 
{ 
    $device = array(); 

    foreach($item as $key => $value) 
    { 
     $device[(string)$key] = (string)$value; 
    } 

    $devices[] = $device; 
} 

print_r($devices); 

輸出:

$xml = simplexml_load_string($xmlString); 
foreach($xml->products->item as $item) 
{ 
    echo (string)$item->product_id; 
    echo (string)$item->model; 
} 

建設設備關聯數組的例子

Array 
(
    [0] => Array 
     (
      [product_id] => 32417 
      [manufacturer] => Alcatel 
      [model] => Sparq 2 
      [deeplink] => http://www.mysite.com/sc_offer?gid=32417 
      [thumbnail_URL] => http://www.mysite.com/images/devices/thumbs/Alcatel-Sparq-II.jpg 
      [image_URL] => http://www.mysite.com/images/devices/Alcatel-Sparq-II.jpg 
      [price_not_working] => 0.00 
      [price_poor] => 0.00 
      [price_fair] => 20.00 
      [price_good] => 25.00 
      [price_perfect] => 25.00 
      [price_new] => 25.00 
      [battery_new] => 1.00 
      [battery_perfect] => 1.00 
      [battery_good] => 1.00 
      [battery_fair] => 1.00 
      [battery_poor] => 0.00 
      [charger_new] => 1.00 
      [charger_perfect] => 1.00 
      [charger_good] => 1.00 
      [charger_fair] => 1.00 
      [charger_poor] => 0.00 
      [packaging_new] => 1.00 
      [packaging_perfect] => 1.00 
      [packaging_good] => 1.00 
      [packaging_fair] => 1.00 
      [packaging_poor] => 0.00 
     ) 

    [1] => Array 
     (
      [product_id] => 31303 
      [manufacturer] => Apple 
      [model] => iPhone 3G 8gb 
      [deeplink] => http://www.mysite.com/sc_offer?gid=31303 
      [thumbnail_URL] => http://www.mysite.com/images/devices/thumbs/iPhone 8 3G.jpg 
      [image_URL] => http://www.mysite.com/images/devices/iPhone 8 3G.jpg 
      [price_not_working] => 0.00 
      [price_poor] => 0.00 
      [price_fair] => 7.00 
      [price_good] => 2.00 
      [price_perfect] => 2.00 
      [price_new] => 2.00 
      [battery_new] => 1.00 
      [battery_perfect] => 1.00 
      [battery_good] => 1.00 
      [battery_fair] => 1.00 
      [battery_poor] => 0.00 
      [charger_new] => 1.00 
      [charger_perfect] => 1.00 
      [charger_good] => 1.00 
      [charger_fair] => 1.00 
      [charger_poor] => 0.00 
      [packaging_new] => 1.00 
      [packaging_perfect] => 1.00 
      [packaging_good] => 1.00 
      [packaging_fair] => 1.00 
      [packaging_poor] => 0.00 
     ) 

) 
+0

+1爲額外的代碼:) – Zabs

1

我不想破壞the existing answer因爲它以一般的方式回答正確的答案。

爲了您具體要求與你的XML不會有任何屬性,你就只爲元素名稱=>節點值這裏雙,有可能想到的一個功能在這裏與SimpleXMLElement結合使用:get_object_vars

當你轉換一個對象到一個數組它是有用的,爲的SimpleXMLElement變成元素名稱爲對象屬性名稱和節點值的屬性值是相當直截了當這裏:

$xml  = simplexml_load_string($buffer); 

$items = $xml->products->item; 

$devices = array_map('get_object_vars', iterator_to_array($items, FALSE)); 

print_r($devices); 

輸出是如現有答案中所建議的那樣。在線演示在這裏:https://eval.in/57109

你很可能能夠實現類似的結果投射到數組(如果與SimpleXML不完全相同),但是在這種情況下,因爲我想映射它,所以我需要一個真正的函數。

也有JSON-恩和島區的代碼轉換完成樹,就派上用場了這裏,也加倍:

$xml  = simplexml_load_string($buffer); 

$items = $xml->products; 

$devices = json_decode(json_encode($items), TRUE)['item']; 

輸出再往後就是完全按照現有的答案。在線演示再次在這裏:https://eval.in/57117

希望這是有益的,並拓寬了一點。

+0

高超的+1給予已經回答的問題的額外細節 - 再次感謝! – Zabs