capture the value of an element in array returned by simpleXML in PHP? - php

Any idea how can i get a single value from an element in array returned by an XML which looks like this?
SimpleXMLElement Object
(
[status] => SimpleXMLElement Object
(
[id] => 0
[description] => Success
)
......
I want to capture the [id] and return that value to run tests against it.
the above was captured using the following
$xml = simplexml_load_string($result);
Thanks

Try this $id = $xml->status->id;

Are you looking for something like:
$sxml = new SimpleXMLElement("<statuses><status><id>0</id><description>success</description></status></statuses>");
var_dump((string) $sxml->status[0]->id);

Related

Trying to extract value from Soap XML output

I'm using PHP SoapClient to call a SOAP service.
I'm using the following code to return the below view
$response = $client->Get($request);
echo '<pre>';
print_r($response);
echo '</pre>';
echo '<hr>';
Here's the response
stdClass Object
(
[TransactionID] => 17ACE7B75CB6SDBX
[ResponseType] => SYNC
[Parameters] => stdClass Object
(
[Param] => stdClass Object
(
[_] => SANDBOX20101001123321125.168.214.72125.168.253.29912003D60E04CC4C1F8
[id] => SessionLogRecord
)
)
)
Here is , showing the elements within it that I want to extract the values of
<onlinesessionrecord><serviceid>SANDBOX</serviceid><datetime>20101001123321</datetime><ipaddress>125.168.214.72</ipaddress><nasipaddress>125.168.253.2</nasipaddress><nasport>9912</nasport><sessionid>003D60E04CC4C1F8</sessionid></onlinesessionrecord>
For example, I am trying to extract from above
I have tried to display it using the following, but I get an error "Attempt to read property "onlinesessionrecord" on string"
$data = $response->Parameters->Param->_->onlinesessionrecord->ipaddress;
print_r($data);
If anyone knows what I'm doing wrong, your advice would be appreciated.
I am sure this is very simple, I just seem to struggle with xml.
--- Next Day, further findings after help from comments ---
So, thanks to the comments below, I have now go to this point. If I use the below:-
$data = simplexml_load_string($response->Parameters->Param->_);
$out = $data->IPAddress; // same if I do the following $data[0]->IPAddress;
print_r($out);
I get this:-
SimpleXMLElement Object ( [0] => 125.168.214.72 ) if I do the below.
But I just want to extract the IPAddress value and assign it to a variable. I have tried 2 ways
$out = $data->IPAddress;
and
$out = $data[0]->IPAddress;
Both give the same output, I just want to actual IPAddress and assign it to a variable.

PHP wildcard for object key

I sometimes encounter and object like this:
stdClass Object
(
[batchcomplete] =>
[query] => stdClass Object
(
[pages] => stdClass Object
(
[48548] => stdClass Object
(
[pageid] => 48548
[ns] => 0
[title] => Dopamine
That object key 48548 is gonna be different every time so I have no way of knowing what its value is. Lets say I need to get the title (Dopamine) in this object, I would need to do something like this:
$title = $object->query->page->{*WILDCARD*}->title;
But I haven't figured out a way to do this yet. Is there a way to skip an object key like this without having to find out the value of the key?
A numeric object property is not going to work. Assuming there is only one, convert to an array and get the key:
$array = (array)$object->query->pages;
$title = $array[key($array)]->title;
Or just get the one element:
$title = current((array)$object->query->pages)->title;
If this is coming from JSON you might want to decode it as an array in the first place. If not, then maybe this:
$array = json_decode(json_encode($oject), true);
For non-numeric properties this should work:
$var = key(get_object_vars($object->query->pages));
$title = $object->query->pages->$var->title;

Using xPath to access values of simpleXML

I have a XML object result from my database containing settings.
I am trying to access the values for a particular settingName:
SimpleXMLElement Object
(
[settings] => Array
(
[0] => SimpleXMLElement Object
(
[settingName] => Test
[settingDescription] => Testing
[requireValue] => 1
[localeID] => 14
[status] => 1
[value] => 66
[settingID] => 5
)
[1] => SimpleXMLElement Object
(
[settingName] => Home Page Stats
[settingDescription] => Show the Top 5 Teammate / Teamleader stats?
[requireValue] => 0
[localeID] => 14
[status] => 0
[value] => SimpleXMLElement Object
(
)
[settingID] => 3
)
)
)
I tried using xPath and have this so far:
$value = $fetchSettings->xpath("//settingName[text()='Test']/../value");
which returns:
Array ( [0] => SimpleXMLElement Object ( [0] => 66 ) )
How can I get the actual value and not just another array/object?
The end result will just be 66 for the example above.
SimpleXMLElement::xpath() returns a plain PHP array of "search results"; the first result will always be index 0 if any results were found.
Each "search result" is a SimpleXMLElement object, which has a magic __toString() method for getting the direct text content of a node (including CDATA, but including text inside child nodes, etc). The simplest way to call it is with (string)$my_element; (int)$my_element will also invoke it, then convert the result to an integer.
So:
$xpath_results = $fetchSettings->xpath("//settingName[text()='Test']/../value");
if ( count($xpath_results) > 0 ) {
$value = (string)$xpath_results[0];
}
Alternatively, the DOMXPath class can return results other than element and attribute nodes, due to the DOM's richer object model. For instance, you can have an XPath expression ending //text() to refer to the text content of a node, rather than the node itself (SimpleXML will do the search, but give you an element object anyway).
The downside is it's rather more verbose to use, but luckily you can mix and match the two sets of functions (using dom_import_simplexml() and its counterpart) as they have the same underlying representation:
// WARNING: Untested code. Please comment or edit if you find a bug!
$fetchSettings_dom = dom_import_simplexml($fetchSettings);
$xpath = new DOMXPath($fetchSettings_dom->ownerDocument);
$value = $xpath->evaluate(
"//settingName[text()='Test']/../value/text()",
$fetchSettings_dom
);
Because every element in a XML-file can appear as multiple times the parser always returns an array. If you are sure, that it is only a single item you can use current()
echo (string) current($value);
Note, that I cast the SimpleXMLElement to a string (see http://php.net/manual/simplexmlelement.tostring.php ) to get the actual value.
Use DomXPath class instead.
http://php.net/manual/en/domxpath.evaluate.php
The sample from php.net is just equivalent what you'd like to achieve:
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
$tbody = $doc->getElementsByTagName('tbody')->item(0);
// our query is relative to the tbody node
$query = 'count(row/entry[. = "en"])';
$entries = $xpath->evaluate($query, $tbody);
echo "There are $entries english books\n";
In this way, you can get values straight from the XML.

query xpath with php

I wrote following php code to extract nodes information from this xml:
<sioctBoardPost rdfabout="http//boards.ie/vbulletin/showpost.php?p=67075">
<rdftype rdfresource="http//rdfs.org/sioc/ns#Post" />
<dctitle>hib team</dctitle>
<siochas_creator>
<siocUser rdfabout="http//boards.ie/vbulletin/member.php?u=497#user">
<rdfsseeAlso rdfresource="http//boards.ie/vbulletin/sioc.php?sioc_type=user&sioc_id=497" />
</siocUser>
</siochas_creator>
<dctermscreated>1998-04-25T213200Z</dctermscreated>
<sioccontent>zero, those players that are trialing 300 -400 pingers? umm..mager lagg and even worse/</sioccontent>
</sioctBoardPost>
<?php
$xml = simplexml_load_file("boards.xml");
$products[0] = $xml->xpath("/sioctBoardPost/sioccontent");
$products[1] = $xml->xpath("/sioctBoardPost/dctermscreated");
$products[2] = $xml->xpath("/sioctBoardPost/#rdfabout");
print_r($products);
?>
This gives following output:
Array (
[0] => Array ( [0] => SimpleXMLElement Object ( [0] => zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet/ ) ) [1] => Array ( [0] => SimpleXMLElement Object ( [0] => 1998-04-25T213200Z ) ) [2] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [rdfabout] => http//boards.ie/vbulletin/showpost.php?p=67075 ) ) )
)
But I need only nodes content as an output i.e without Array([0] => Array etc.
Output should be like this:
zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet
1998-04-25T213200Z
http//boards.ie/vbulletin/showpost.php?p=67075
Thanks in advance
You can use current() to only get the first element of each XPath result (which is an array) and then use a (string) cast to get the node contents:
$products[0] = (string)current($xml->xpath("/sioctBoardPost/sioccontent"));
$products[1] = (string)current($xml->xpath("/sioctBoardPost/dctermscreated"));
$products[2] = (string)current($xml->xpath("/sioctBoardPost/#rdfabout"));
print_r($products);
As you have observed, the xpath() method returns an array of matched nodes, so you need to deal with the elements of the returned arrays. I believe this should work in this case:
$xml = simplexml_load_file("boards.xml");
$products[0] = $xml->xpath("/sioctBoardPost/sioccontent")[0];
$products[1] = $xml->xpath("/sioctBoardPost/dctermscreated")[0];
$products[2] = $xml->xpath("/sioctBoardPost/#rdfabout")[0];
print_r($products);
This should get you what you need...
foreach ($products as $product) { // iterate through the $products array
print $product[0]->nodeValue // output the node value of the SimpleXMLElement Object
}

Getting value from SimpleXMLElement Object

Hi I have this following segment of XML:
......
<Result number="4" position="1" points="25">
<Driver driverId="button" url="http://en.wikipedia.org/wiki/Jenson_Button">
<GivenName>Jenson</GivenName>
<FamilyName>Button</FamilyName>
<DateOfBirth>1980-01-19</DateOfBirth>
<Nationality>British</Nationality>
</Driver>
......
I can use the following easily to get the GivenName:
$item->Driver->GivenName;
But when I use:
$item->Driver->FamilyName;
I get SimpleXMLElement Object ()
I have looked around and found that it might be something to do with passing it to a string but then I get nothing on screen. Not even SimpleXMLElement Object.
I don't understand as it's a sibling of GivenName and that works.
You get a SimpleXMLElement object in both cases, which you'll see if you use print_r():
print_r ($item->Driver->GivenName);
print_r ($item->Driver->FamilyName);
Outputs:
SimpleXMLElement Object
(
[0] => Jenson
)
SimpleXMLElement Object
(
[0] => Button
)
You can use an explicit cast to get the values as strings:
$givenNameString = (string) $item->Driver->GivenName;
$familyNameString = (string) $item->Driver->FamilyName;
To make PHP understand you have to give typecasting forcefully on object like below:
$givenName = (array) $item->Driver->GivenName;
$familyName = (array) $item->Driver->FamilyName;
print_r($givenName);
print_r($familyName);
OUTPUT :
Array ([0] => 'Jenson')
Array ([0] => 'Button')

Categories