如果节点具有属性,如何从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

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

It looks you are using SimpleXML to parse the data. You can look here on how you can get the attributes.

In your case it will look like

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