setting an array from simple_xml data - php

I am trying to create an array of titles from an xml feed using this code:
$url = 'https://indiegamestand.com/store/salefeed.php';
$xml = simplexml_load_string(file_get_contents($url));
$on_sale = array();
foreach ($xml->channel->item as $game)
{
echo $game->{'title'} . "\n";
$on_sale[] = $game->{'title'};
}
print_r($on_sale);
The echo $game->{'title'} . "\n"; returns the correct title, but when setting the title to the array i get spammed with this:
Array
(
[0] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[2] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
Am I missing something when setting this array?

Use this:
$on_sale[] = $game->{'title'}->__toString();
or even better (in my opinion):
$on_sale[] = (string) $game->{'title'};
PHP doesn't know that you want the string value when you add the object to the array, so __toString() doesn't get called automatically like it does in the echo call. When you cast the object to string, __toString() is called automatically.
FYI: You don't really need the curly braces either, this works fine for me:
$on_sale[] = (string) $game->title;

Related

PHP Node no longer exists SimpleXML

I have a SimpleXMLElement object that I got from parsing a response from oData. I'm looping through the children and it works well as long as it has children (the truth is I'm not even sure if they are its children or if its just a collection). This is how my code looks like:
$dataset = soapService->doSoapCall();
$array = $dataset->children()->children();
foreach($rawArray as $element)
{
$row['Something'] = (string)$element->Something;
$newArr[] = $row;
}
The problem is that this code throws an error of: PHP Node no longer exists SimpleXML. I've been trying to check for null or check the count of it but I keep getting the same error. When I look at it from the debugger in net beans it just says its type SimpleXMLElement but I doesn't have any other value or children. Please help.
Note: When I do print_r($array) i get this:
SimpleXMLElement Object ( )
When the object actually has data it returns this:
SimpleXMLElement Object ( [Table] => Array ( [0] => SimpleXMLElement Object ( [EventCode] => 10020 ) [1] => SimpleXMLElement Object ( [EventCode] => 10030 ) [2] => SimpleXMLElement Object ( [EventCode] => 10040 ) ) ) {"code":99200}

getting elemints out of simpleXML Array

I am trying to get elements out of a simpleXML array and for some reason I am unable to call them.
Here is the array.
Array
(
[0] => SimpleXMLElement Object
(
[NameChangeIndicator] => N
[NameChangeDate] => SimpleXMLElement Object
(
)
[PreviousName] => SimpleXMLElement Object
(
)
[Score] => 53
[NumberOfSubs] => SimpleXMLElement Object
(
)
[NumberOfJU] => SimpleXMLElement Object
(
)
[DateLastJU] => SimpleXMLElement Object
(
)
[NumberActPrincipals] => 1
[NumberActPrincipalsJU] => SimpleXMLElement Object
(
)
[LastestBankCode] => SimpleXMLElement Object
(
)
[LastestBankCodeDate] => SimpleXMLElement Object
(
)
[NumberRDs] => SimpleXMLElement Object
(
)
[LiqIndicator] => SimpleXMLElement Object
(
)
[TotEnqLast12Mth] => SimpleXMLElement Object
(
)
[TotEnqLast3Mth] => SimpleXMLElement Object
(
)
[RefsNoOfReferences] => SimpleXMLElement Object
(
)
[RefsHighMthPurchases] => SimpleXMLElement Object
(
)
[RefsHighMthPurchasesTermGiven] => SimpleXMLElement Object
(
)
[RefsHighMthPurchasesTermTaken] => SimpleXMLElement Object
(
)
[RefsLowMthPurchases] => SimpleXMLElement Object
(
)
[RefsLowMthPurchasesTermGiven] => SimpleXMLElement Object
(
)
[RefsLowMthPurchasesTermTaken] => SimpleXMLElement Object
(
)
[KissNoOfSuppliers] => SimpleXMLElement Object
(
)
[KissNoOfODSuppliers] => SimpleXMLElement Object
(
)
[KissAmountOS] => SimpleXMLElement Object
(
)
[KissAmountOD] => SimpleXMLElement Object
(
)
[KissPercntage] => SimpleXMLElement Object
(
)
[LatestBankCodeDesc] => SimpleXMLElement Object
(
)
[HoldingCmpName] => SimpleXMLElement Object
(
)
)
)
So I am doing the following call to get the array.
$new_str = htmlspecialchars_decode($str);
$new_str = str_replace('<?xml version="1.0" encoding="UTF-8"?>','',$new_str);
$xml = simplexml_load_string($new_str);
$dom = new SimpleXMLElement($new_str);
$xml_array = $dom->xpath("//*[name()='ReportSummary']");
echo "{$xml_array[0]['Score']}";
But I am unable to pull the object out of the Array. I am not sure if the array
is being correctly sent back to me due to the fact that if I don't decode the string I don't get a array back. The weird thing is that in the array I keep on seeing "SimpleXMLElement Object" and I am not sure if that is correct.
Any help will be appreciated.
As the dump output says, SimpleXML is a type of object, not a way of creating arrays.
These two lines are different ways of writing the same thing, you only need one of them; in either case you end up with a SimpleXMLElement object:
$xml = simplexml_load_string($new_str);
$xml = new SimpleXMLElement($new_str);
The outer array you are seeing is to hold the results of the XPath query, since they can come from anywhere in the XML tree. It is an array of SimpleXMLElement objects.
For how to access data using SimpleXML, see the basic usage page in the PHP manual.
In your case, Score is an element of the document, so needs to be accessed with the $node->property syntax.
Here's a tidied up version of your code:
$new_str = htmlspecialchars_decode($str);
// Are you sure the next line is necessary? That looks like a valid XML opening to me.
$new_str = str_replace('<?xml version="1.0" encoding="UTF-8"?>','',$new_str);
$xml = simplexml_load_string($new_str);
// I think this simpler XPath expression means the same as yours, but I might be wrong
$xpath_results = $xml->xpath('//ReportSummary');
// Beware that the XPath could return no matches, in which case the following
// would give an error. Best to check count($xpath_results) > 0 first.
echo $xpath_results[0]->Score;
To IMSoP: thanks for the help but I had to modify it a bit:
$dom = new SimpleXMLElement($new_str);
$xml_array = $dom->xpath("//*[name()='ReportSummary']");
echo $xml_array[0]->Score;
And from that I got the correct result, thanks a lot!

Reading XML file - strange PHP object notation

I have a XML file simillar to this :
<information version="2">
<currentTime>2014-06-06 17:28:16</currentTime>
<result>
<name>Mark</name>
<surname>Smith</surname>
</result>
I read it with php function and parse it to the object with function, like this:
function parse_data($data){
$return_data['currentTime'] = $data->currentTime;
$return_data['name'] = $data->result->name;
$return_data['surname'] = $data->result->surname;
return $return_data;
}
$xml = simplexml_load_string(file_get_contents($link));
$object = parse_data($xml);
Then, when I echo it on the screen, to check how it look:
//json_encode($xml);
{
"#attributes":{"version":"2"},
"currentTime":"2014-06-06 17:28:16",
"result":{"name":"Mark","surname":"Smith"}
}
//print_r($xml);
SimpleXMLElement Object (
[#attributes] => Array ( [version] => 2 )
[currentTime] => 2014-06-06 17:56:30
[result] => SimpleXMLElement Object (
[name] => Mark
[surname] => Smith
)
)
//json_encode($object);
{
"currentTime":{"0":"2014-06-06 17:28:16"},
"name":{"0":"Mark"},
"surname":{"0":"Smith"}
}
//print_r($object);
Array (
[currentTime] => SimpleXMLElement Object ( [0] => 2014-06-06 17:52:50 )
[name] => SimpleXMLElement Object ( [0] => Mark)
[surname] => SimpleXMLElement Object ( [0] => Smith )
)
What is wrong with my code? He seems to read the informaton in xml file as array? Because of this strange notation I cannont operate on this data normally.
It also behave like this:
echo json_encode($object['name']); will give -> {"0":"Mark"}
echo $object['name']; will give -> Mark
Can anybody help me? What am I doing wrong?
I want my $object to look like this:
//json_encode($object);
{
"currentTime":"2014-06-06 17:28:16",
"name":"Mark",
"surname":"Smith"
}
Edit1: added print_r values
Yes, as you have noticed the type returned by $someSimpleXMLNode is an object. If you want the node value (as a string for example) use:
$return_data['currentTime'] = (string)$data->currentTime;
which is the same as doing
$return_data['currentTime'] = $data->currentTime->__toString();
etc
When you do
echo $data->currentTime;
the node is automatically coerced into a string (because echo only handles strings). This is done (generally, in php) by the object's __toString method.

Get Methods from REST call

I would like to get methods from REST XML file via PHP.
I have local REST file, which is in this format:
SimpleXMLElement Object
(
[doc] => SimpleXMLElement Object
(
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[base] => https://**url**
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => xml/{accesskey}/project
)
[param] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => accesskey
[style] => template
[type] => xs:string
)
)
[method] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => getAllProjects
[name] => GET
)
[response] => SimpleXMLElement Object
(
[representation] => SimpleXMLElement Object
(
[#attributes] => Array
(
[mediaType] => application/xml; charset=utf-8
)
)
)
)
... and so on
I have the following code, but it returns just the first method name:
$file="application.wadl";
$xml = simplexml_load_file($file);
foreach($xml->resources[0]->resource->method->attributes() as $a => $b) {
echo $b,"\n";
}
I would like to extract all of them, not just the first one. How to do that?
Rather than looping over the attributes of one element, you need to loop over all the elements with the same name. Due to the magic of SimpleXML, this is as simple as this:
foreach($xml->resources->resource->method as $method) {
echo $method['id'],"\n";
}
When followed immediately by another operator, as with ->resources, SimpleXML assumes you just want the first element with that name. But if you loop over, it will give you each of them, as a SimpleXML object.
EDIT : It looks like the nesting of your XML means you need some form of recursion (you need to look at $xml->resources->resource->resource->resource->method etc).
Something like this perhaps (untested example)?
function get_methods($base_url, $node)
{
$all_methods = array();
// Child resources: build up the path, and recursively fetch all methods
foreach ( $node->resource as $child_resource )
{
$child_url = $base_url . '/' . (string)$child_resource['path'];
$all_methods = array_merge(
$all_methods,
get_methods($child_url, $child_resource)
);
}
// Methods in this resource: add to array directly
foreach ( $node->method as $method )
{
$method_url = $base_url . '/' .(string)$method['id'];
$all_methods[$method_url] = (string)$method['id'];
}
return $all_methods;
}
print_r( get_methods('/', $xml->resources) );
Incidentally, print_r won't always give you the best view of a SimpleXML object, because they are actually wrappers around non-PHP code. Try this simplexml_dump() function instead.

SimpleXMLElement array retrieval issue

How to get Cxyabc, Cxy123 and Cxy234 inside an array from below object?
$xml_element = simplexml_load_string($xml,null, LIBXML_NOCDATA);
$childId = $xml_element->Parent->ChildID;
print_r(childId);
SimpleXMLElement Object (
[#attributes] => Array (
[entity] => result
[order-value] => 1
)
[0] => Cxyabc
[1] => Cxy123
[2] => Cxy234
)
Thanks for answers, i tried below one and working fine. string conversion is necessary.
$test = array();
foreach($childId as $value){
$strValue = (string)$value;
array_push($test,$strValue);
}
Try:
$cxyabc = $obj->{0};
$cxy123 = $obj->{1};
The usage of { } is necessary because object properties cannot begin with a digit so $obj->0 is not valid.
You would access the attributes using array notation:
$entity = $obj['entity'];

Categories