Extract data from an XML object
How do i extract the data from that xml object which is a value of a certain array:
Array ( [Tit开发者_运维知识库le] => SimpleXMLElement Object (
[0] => The Key of Life; A Metaphysical Investigation )
[ASIN] => SimpleXMLElement Object ( [0] => 0982385099 ) ...
Do string typecasting of the object.
$variable = (string) $FieldValue[0];
That would work, as SimpleXml has all the children in object type and not string.
say $X is the object, so to print
The Key of Life; A Metaphysical Investigation
you do:
echo $X->Title[0]
It's important to understand that you're not working with an array — you're working with a SimpleXMLElement object, which is not the same.
Instead of doing
$array['key']['subkey']
, you would do$xml->tag->subtag
.SimpleXML nodes are not strings or arrays, although they behave string-like and array-like. Make sure you always typecast the value to an explicit string.
If you're accessing the first node, you don't need to use
[0]
. It's assumed.You can convert SimpleXMLElement objects into true associative arrays in PHP 5.2 or newer with:
$array = json_decode(json_encode($xml), true);
精彩评论