Extract data from an XML object - php

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

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

Related

Getting the name of a stdClass object in PHP

I'm playing with an API that's giving me data back in JSON format that I then json_decode() to the following format:
[stockData] => stdClass Object
(
[data] => stdClass Object
(
[PS3] => stdClass Object
(
[2015-01-26T20:45:01Z] => stdClass Object
(
[AMU] => 999.76
[ZIT] => 3.63
)
)
)
[status] => stdClass Object
(
[code] => 200
[text] => ok
)
)
I need some way of getting the 2015-01-26T20:45:01Z (which changes all the time).
I've tried get_Class() on the object, eg:
get_Class($bawsaq->stockData->data->PS3) (actually in a foreach loop)
But all that's returned is: "stdClass" and not the name. How can I get the object's name?
It isn't actually the object's class: it's the name of the property that contains the stdClass object. So you'd need to get the first object property name from $bawsaq->stockData->data->PS3. Which is a bit tricky, actually.
It's nicer to work with arrays. If you use the $assoc parameter of json_decode, you can get an associative array instead of an object whenever a JSON object appears. This is much easier to deal with in PHP.
$bawsaq = json_decode($jsonData, true);
You can get the key name with key:
$dateTime = key($bawsaq['stockData']['data']['PS3']);
When you decode the JSON, use
$bawsaq = json_decode($json, true);
This will return associative arrays instead of stdClass objects for all the JSON objects. Then you can use
$keys = array_keys($bawsaq['stockData']['data'];
$date = $keys[0];
You can use get_object_vars method.
$obj = new stdClass();
$obj->field1 = 'value1';
print_r(get_object_vars($obj));
Result:
Array
(
[field1] => value1
)
You can use the second argument to json_decode. This will return the data as an associative array instead of an object list, so you could simply use
$input = json_decode($jsonInput, true);
$key = key($input['stockData']['data']['PS3']);
$data = $input['stockData']['data']['PS3'][$key];
or a foreach-loop. See also key on php.net.

How to retrieve #attributes array value?

Can someone help me retrieve the value of the id from this array? Thanks
Array
(
[#attributes] => Array
(
[id] => 1
)
)
This is a SimpleXML element. The #attributes key is just how it's set internally. Don't try to access it via that key.
You can just simply get the attribute by doing:
$id = (string)$element['id'];
Note: The cast to a string is needed because SimpleXML gives you a SimpleXMLElement object when you access something.

PHP Simple XML Element Object - How to get string value

How do I get the values of the follow array fields in php? When I do a print_r function on a two dimensional array, I get the following result:
[title] => SimpleXMLElement Object ( ) [date] => SimpleXMLElement Object ( )
How do I get the string value of title and date? I've tried casting them with
(string)
, however this didn't work.
You need to access the attributes of the SimpleXMLElement Object.
Say you have an instance named entry with child title you type
(string) $entry->title;

Reading XML and storing to arrays in PHP

I have this:
$xml = simplexml_load_file('test.xml');
print"<pre>";
print_r($xml);
It printout this:
SimpleXMLElement Object
(
[b] => SimpleXMLElement Object
(
[c] => SimpleXMLElement Object
(
[d] => 543
)
)
)
but when I type echo $xml["b"]["c"]["d"]; nothing happens
the print_r is kind of misleading,
actually the $xml is series/array of SimpleXmlElement objects
so
echo (int)$xml->b->c->d; --- type casting is required
here is some reference you should take a look first
Additional to type casting,
because everything node inside the xml object is either string or int
PHP will auto convert for numeric string to integer,
however, is clearer if you provide the type hinting
var_dump($xml); --- you should see more information on the data type
Try echo $xml->b->c->d;
$xml is not array they are objects.

Why aren't these values being added to my array as strings?

Further to my question here, I'm actually wondering why I'm not getting strings added to my array with the following code.
I get some HTML from an external source with this:
$doc = new DOMDocument();
#$doc->loadHTML($html);
$xml = #simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath('//img');
$sources = array();
Here is the images array:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[alt] => techcrunch logo
[src] => http://s2.wp.com/wp-content/themes/vip/tctechcrunch/images/logos_small/techcrunch2.png?m=1265111136g
)
)
...
)
Then I added the sources to my array with:
foreach ($images as $i) {
array_push($sources, $i['src']);
}
But when I print the results:
echo "<pre>";
print_r($sources);
die();
I get this:
Array
(
[0] => SimpleXMLElement Object
(
[0] => http://www.domain.com/someimages.jpg
)
...
)
Why isn't $i['src'] treated as a string? Isn't the original [src] element noted where I print $images a string inside there?
To put it another way $images[0] is a SimpleXMLElement, I understand that. But why is the 'src' attribute of THAT object not being but into $sources as a string when I reference it as $i['src']?
Why isn't $i['src'] treated as a string?
Becaue it isn't one - it's a SimpleXMLElement object that gets cast to a string if used in a string context, but it still remains a SimpleXMLElement at heart.
To make it a real string, force cast it:
array_push($sources, (string) $i['src']);
Because SimpleXMLElement::xpath() (quoting) :
Returns an array of SimpleXMLElement
objects
and not an array of strings.
So, the items of your $images array are SimpleXMLElement objects, and not strings -- which is why you have to cast them to strings, if you want strings.

Categories