2012-09-26 89 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? –

相关问题