开发者

Extract data from an XML object

开发者 https://www.devze.com 2023-02-08 18:09 出处:网络
How do i extract the data from that xml object which is a value of a certain array: Array ( [Tit开发者_运维知识库le] => SimpleXMLElement 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.

  1. Instead of doing $array['key']['subkey'], you would do $xml->tag->subtag.

  2. 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.

  3. If you're accessing the first node, you don't need to use [0]. It's assumed.

  4. You can convert SimpleXMLElement objects into true associative arrays in PHP 5.2 or newer with:

    $array = json_decode(json_encode($xml), true);

0

精彩评论

暂无评论...
验证码 换一张
取 消