dougu3988 2012-09-26 10:42 采纳率: 100%
浏览 29
已采纳

如果节点具有属性,如何从xml中检索值? [重复]

Possible Duplicate:
How to get an attribute with SimpleXML?

How can I get the values of height,length,weight and width? since it has attributes on it? In other values I have no problem in retrieving it, just this values has attributes.

Note:The height,length,weight and width part is only the problem

Here's How I retrieve it:

$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

  • 写回答

2条回答 默认 最新

  • dsagzmosl32217092 2012-09-26 11:19
    关注

    The simplexml library will return you the object for that node. But you only want it's text. Therefore you need to cast it to string:

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

    That will give you the text as string of that XML node.


    In the Simplexml documentationDocs you find that in Example #6 Comparing Elements and Attributes with Text:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了