2012-09-26 87 views
2

Possible Duplicate:
How to get an attribute with SimpleXML?如果節點具有屬性,如何從xml檢索值?

我怎樣才能得到高度,長度,重量和寬度的值?因爲它具有屬性呢?在其他值中,我沒有檢索它的問題,只是這個值有屬性。

注:高度,長度,重量和寬度的部分僅是問題

這裏是我如何找回它:

$from_amazon = array(
    'asin'  => $item->ASIN, 
    'product_name' 
       => $item->ItemAttributes->Title, 
    'image' => $item->SmallImage->URL, 
    'price' => $item->ItemAttributes->ListPrice->FormattedPrice, 
    'product_description' 
       => $item->EditorialReviews->EditorialReview->Content, 
    'category' => $item->ItemAttributes->ProductGroup, 
    'weight' => $item->ItemAttributes->PackageDimensions->Weight 
); 

XML DOM

+0

我不明白呢? :( –

+1

再試一次,看看下面答案中代碼行的變化如果你想了解更多,你應該閱讀關於simplexml的手冊,它有一些基本的例子:http://php.net/manual /en/simplexml.examples-basic.php,其中包含你需要知道的所有東西,特別是* Example#5使用屬性*有你要找的東西 – hakre

+0

在我的回覆中,權重值就像這個「weight」:{ @attributes「:{」Units「:」百分之一磅「},」0「:」420「}} –

回答

1

的simplexml的庫將返回的對象爲節點。但你只想要它的文字。因此,你需要將其轉換爲字符串:

'weight' => (string) $item->ItemAttributes->PackageDimensions->Weight 
       ^^^^^^^^ 

這會給你的文本作爲XML節點的字符串。


Simplexml documentationDocs你會發現,在例#6比較元件和與文本屬性:

To compare an element or attribute with a string or pass it into a function that requires a string, you must cast it to a string using (string). Otherwise, PHP treats the element as an object.

(string) $movies->movie->title 
+0

非常感謝! :) –

1

看起來你正在使用SimpleXML來解析數據。 你可以看看here關於如何獲取屬性。

在你的情況下,它看起來像

$item->ItemAttributes->PackageDimensions->Weight->attributes()->Units 
+0

致命錯誤:調用一個非對象的成員函數attributes() –

+0

對不起,更新了代碼 – zysoft

+0

你是什麼chnge? –

相關問題