php - 如果节点具有属性,如何从 xml 检索值?

标签 php xml simplexml

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
);

XML DOM

最佳答案

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/

相关文章:

php - 如何将 JSON 字符串转换为数组(PHP)?

php - 通过 php 的 exec() 命令执行以下命令

php - SSL 错误 : Secure Connection Failed

json - 使用 XQuery 检查 XML 文档的命名空间前缀是否在整个文件中被重新定义

php - simplexml_load_file() 错误

php - 简单的 XML 加载文件不起作用

php - 获取 xml 文件以查找和替换文本。 PHP

php - 干预图像 - 如何上传 MIME 类型为 : application/octet-stream 的图像

xml - XSD:默认整数值范围

android - 按钮未显示在 Android Studio 中