Possible Duplicate:
How to get an attribute with SimpleXML?
如何获取高度、长度、重量和宽度的值?因为它有属性吗?在其他值中,我检索它没有问题,只是这个值具有属性。
注意:高度、长度、重量和宽度部分只是问题
这是我检索它的方法:
$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
);
最佳答案
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
关于php - 如果节点具有属性,如何从 xml 检索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12599924/