2

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

Community
  • 1
  • 1
Sui Go
  • 463
  • 2
  • 12
  • 31
  • 1
    Try again, see the changes in the line of code in the answer below. If you want to learn more, you should to read the manual about simplexml, it has some basic examples: http://php.net/manual/en/simplexml.examples-basic.php that contains everything you need to know. Especially *Example #5 Using attributes* has what you're looking for. – hakre Sep 26 '12 at 11:02
  • in my response the weight value is like this "weight":{"@attributes":{"Units":"hundredths-pounds"},"0":"420"}} – Sui Go Sep 26 '12 at 11:12
  • 1
    Okay, then try: `'weight' => (string) $item->ItemAttributes->PackageDimensions->Weight`. The important part is to *cast to string*, see the `(string)` in front of the `$item->...`. Actually, this is a different duplicate. – hakre Sep 26 '12 at 11:18
  • @hakra yey it works! yo save me – Sui Go Sep 26 '12 at 11:19

2 Answers2

1

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
zysoft
  • 2,268
  • 1
  • 16
  • 21
  • Fatal error: Call to a member function attributes() on a non-object – Sui Go Sep 26 '12 at 10:59
  • @hakra Yes, but in his structure he has `Units` attribute of `Width` which is not what the code says now. I think either mine example should be fixed to match question XML or the question XML should be clarified. – zysoft Sep 26 '12 at 11:12
  • it has warning and in my response weight: null? – Sui Go Sep 26 '12 at 11:14
  • @SuiGo I've written it originally as `$item->ItemAttributes->PackageDimensions->Weight->attributes()->Units`. Please try it. – zysoft Sep 26 '12 at 11:16
  • Fatal error: Call to a member function attributes() on a non-object – Sui Go Sep 26 '12 at 11:16
  • @SuiGo Then probably you should update the question with the correct XML. For example in your code you shown `$item->ItemAttributes->Title` but in your XML there's no `Title` tag in `ItemAttributes`. – zysoft Sep 26 '12 at 11:20
  • Thanks you for helping me appreciate it :) – Sui Go Sep 26 '12 at 11:20
1

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
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836