简体   繁体   中英

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 ( [Title] => SimpleXMLElement Object ( 
[0] => The Key of Life; A Metaphysical Investigation ) 
[ASIN] => SimpleXMLElement Object ( [0] => 0982385099 ) ...

say $X is the object, so to print

The Key of Life; A Metaphysical Investigation

you do:

echo $X->Title[0]

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM