2017-02-12 118 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