php simplexml get object variable start with number - php

When I parse an rss file with SimpleXMLElement, I get this object :
object(SimpleXMLElement)#307 (1) {
[0]=>
string(39) "http://workspace/wordpress/hello-world/"
}
$var->0 doesn't works.
I don't know how to do it :(
thanks.

It behaves like an array
echo $var[0];
In this case - when there's only one (child) element - you don't even have to use the index
echo $var;

Related

PHP: get data from XML string

I have a simple problem getting data from an eBay API string. I want to format numbers to 2 digits 8.0 > 8.00
This works fine
echo $price; // output: 8.0
But...
echo number_format($price, 2); // output: (nothing)
A var_dump tells me why...
var_dump($price);
// output: object(SimpleXMLElement)#19 (2) { ["#attributes"]=> array(1) { ["currencyId"]=> string(3) "USD" } [0]=> string(3) "8.0" }
How do I get the 8.0 into a 8.00 (I know I can use REGEX but it feels like not the proper way)
And while we are here, how I can get the 'USD' ?
PS: the API call used is findCompletedItems - and strangely to me, the XML response has no visible USD at all.
The var_dump gives you an object of type SimpleXMLElement which has a __toString method which returns the text content that is directly in the element so echo $price; will result in 8.0
The USD is part of the attributes which returns an object of type SimpleXMLElement.
You can get the price and the currency casting it to a (string)
$priceAsString = (string)$price;
$currencyIdAsString = (string)$price->attributes()->currencyId;
You're not passing in a string, you're passing in an object of class SimpleXMLElement. The easiest you can do is cast it to a string before passing it to number_format using (string)$price

trying to get an element from an array in php

i have an array after convert it from xml data and i have var_dump the data in array like :
object(SimpleXMLElement)#651 (1) {
["authenticationSuccess"]=>
object(SimpleXMLElement)#652 (1) {
["user"]=>
string(9) "yassine"
}
}
i want to get the value the attribut user that equal "yassine" in this cas .
i trying
$xml["authenticationSuccess"]["user"]
but not working , it return null value , is there any solution to get this data from the array .
some help please
It seems your variable is not array but object, so you need to use $xml->authenticationSuccess->user;
As the var_dump says, you have an object instead of an associative array. You can access object fields like this:
$xml->authenticationSuccess->user;
or this:
$xml->{"authenticationSuccess"}->{"user"};

Getting the value of XML field matching a property via XPATH

I've got a really odd XML schema that's causing me unecessary grief and woe.
I need to get the value of an IMAGEFILENAME node that has a property of "hide".
The XML schema looks something like:
<PHOTOS>
<IMAGETHUMBFILENAME/>
<IMAGECAPTION>
This is a caption
</IMAGECAPTION>
<PRINTQUALITYIMAGE>
/mylocation/filename1.jpg
</PRINTQUALITYIMAGE>
<IMAGEFILENAME pictype="show">
/mylocation/filename2.jpg
</IMAGEFILENAME>
<IMAGETHUMBFILENAME/>
<IMAGECAPTION>This is another caption</IMAGECAPTION>
<PRINTQUALITYIMAGE>
/mylocation/filename3.jpg
</PRINTQUALITYIMAGE>
<IMAGEFILENAME pictype="hide">
/mylocation/filename4.jpg
</IMAGEFILENAME>
<IMAGETHUMBFILENAME/>
</PHOTOS>
And I've managed to come up with the following XPATH using PHP:
$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[#pictype="hide"]');
var_dump($nodes);
When I do a dump of the $nodes var what I'd hope to see (and what I want) is to get the value /mylocation/filename4.jpg. Instead what I'm getting is:
array(1) {
[0]=>
object(SimpleXMLElement)#333 (1) {
["#attributes"]=>
array(1) {
["pictype"]=>
string(10) "hide"
}
}
}
I've tried various combinations of /parent, /text() and /node() but with no joy at all.
Please somebody tell me what a muppet I'm being and put me out of my misery. Either that or is the schema being problematic?
So, you have array of SimpleXMLElements.
To get string representation of SimpleXMLElement you can just echo it:
$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[#pictype="hide"]');
echo $nodes[0]; // I used `[]` notation to get first element of array
To use string representation of SimpleXMLElement later in your code you can convert it to string explicitly:
$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[#pictype="hide"]');
$node_str = strval($nodes[0]); // still `[]` notation

Accessing a specific object by name/number?

I'm returning some data, and testing my return by using var_dump($this);. It returns the following:
object(ReportingService)#333 (2) {
["_arrErrors"]=>
array(0) {
}
["nameWS"]=>
string(14) "reportingstuff"
}
{"arrMessages":[{"_strMessage":"Example.","_strType":"valid","_strModule":null}],"arrContent":{"isSuccess":"1","statistics":"<div id=\"entities\">
What I am trying to do is to access the nameWS property of the object, but cannot seem to do so.
What I've tried:
var_dump($this[0]->nameWS);
Use the following:
$this->nameWS;
You find more information in the manual Setting and Getting Object Properties
You can access it via:
$this->nameWS;
The zero you were using is for the array in "_arrErrors", so you don't need that.
echo $this->nameWS;
will output:
reportingstuff

Access Object Value

I am trying to pull out a value from inside a larger object. The main object from an xml file via SimpleXML.
When I var_dump($data->extensions->runTime); this section of the object I get:
object(SimpleXMLElement)#21 (1) {
[0]=>
string(8) "2852.462"
}
How can I access that 2852.462??
I have tried everything I can think of, via array [0], even with a foreach statement. I can't figure out how to access only the value.
Cast it to string:
$value = (string)$data->extensions->runTime[0];
Or better to float:
$value = (float)$data->extensions->runTime[0];

Categories