2017-02-12 77 views
0

試圖通過以下鏈接刮宜家頁:試圖從XML屬性的特定值

http://www.ikea.com/it/it/catalog/products/60255550/?type=xml&dataset=prices 

我想刮商品的價格,但在XML文件中的價格出現一次未格式化並且旁邊有一個歐元符號。我希望特別提供價格正常的無格式值。

<prices> 
<normal> 
<priceNormal unformatted="44.99">€ 44,99</priceNormal> 
<pricePrevious/> 
<priceNormalPerUnit/> 
<pricePreviousPerUnit/> 
</normal> 

我下面的代碼不會回價格在所有的,不知道我要去哪裏錯了:(

$string = 'http://www.ikea.com/it/it/catalog/products/60255550/?type=xml&dataset=prices'; 

$xml=simplexml_load_file($string) or die("Error: Cannot create object"); 
//print_r($xml); 

echo $xml->product->prices; 

回答

1

你應該能夠得到的價格與

$xml->products->product->items->item->prices->normal->priceNormal 
$xml->products->product->items->item->prices->normal->priceNormal->attributes()->unformatted 

不過,若你需要遍歷結果集,你可以打破你在哪裏使用迭代期望倍數的地方......

foreach($xml->products->product as $product) 
{ 
    echo $product->name; 
    foreach($product->items->item as $item) 
    { 
    echo $item->name; 
    echo $item->prices->normal->priceNormal; 
    echo $item->prices->normal->priceNormal->attributes()->unformatted; 
    } 
} 
1

嘗試使用的var_dump()代替print_r()來看待$xml值。這是一個有點令人費解,但你會發現你在這個位置尋找的數據:

$xml->products[0]->product->items[0]->item->prices->normal->priceNormal[0]; 
+0

返回的值仍然具有連接到歐元符號它,我試圖避免這一點,有什麼建議嗎? – Massive

+1

在@ miken32的答案末尾使用'priceNormal [0] - > attributes() - > unformatted'。 – Scuzzy