Get Value from Simplexmlelement Object

Getting values from simplexmlelement object

Try

$str = <<<XML
<affiliate_signup_response>
<success>false</success>
<message>Duplicate Affiliate Contact</message>
<affiliate_id>0</affiliate_id>
</affiliate_signup_response>
XML;


$xml = new SimpleXMLElement($str);
//var_dump($xml);
$success = $xml->success;
$message = $xml->message;
$affiliate_id = $xml->affiliate_id;

echo $success."<br />";
echo $message."<br />";
echo $affiliate_id."<br />";

Pull values from SimpleXMLElement Object

Learning to use SimpleXML is much better than trying to convert it to arrays/json/anything else and simple (hence the name). A quick example...

$response = '<?xml version="1.0" encoding="UTF-8"?>
<CommandResponse Type="namecheap.domains.check">
<DomainCheckResult Domain="facebook.com">
<Element>1234</Element>
<Element>12345</Element>
</DomainCheckResult>
</CommandResponse>';
$xml = simplexml_load_string($response);

echo "DOmain=".$xml->DomainCheckResult['Domain'].PHP_EOL;
foreach ( $xml->DomainCheckResult->Element as $value) {
echo "Value=".(string)$value.PHP_EOL;
}

outputs...

DOmain=facebook.com
Value=1234
Value=12345

You have to adapt this to your own XML, but the idea is that if you want to access an element of an item you use object notation -> and if you need to get an attribute, use array notation [].

So in the above code, the first echo ($xml->DomainCheckResult['Domain']) gets the <DomainCheckResult> element and outputs the Domain attribute.

Then the foreach loop says fetch each <Element> within <DomainCheckResult> and output the value.

Get attribute value in SimpleXML object by element and another attribute value

You can use SimpleXML's xpath method to return an attribute of a node based on the value of another attribute:

$sxml = simplexml_load_string($xml);

$rate = (float) $sxml->xpath('./currencies/currency[@id="EUR"]/@rate')[0];

echo $rate;

Note that the method will always return an array, so we need to ask for the first element, and then cast the value to a float.

See https://eval.in/957883 for a full example

Get values from SimpleXMLElement Object for php array

You are using the same variable name for two different things:

foreach($xml->item as $rate){
// at this point, $rate is the <item> element

$rate = (string) $rate->exchangeRate;
// now $rate is a string with the exchange rate

$curr_code = (string) $rate->targetCurrency;
// so now this won't work

$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}

If you were running with display_errors switched on or checking your logs, you would have seen a message like this:

Notice: Trying to get property 'targetCurrency' of non-object

Or in PHP 8, this:

Warning: Attempt to read property "targetCurrency" on string


The fix is simply to name your variables more carefully:

foreach($xml->item as $itemElement){
$rate = (string) $itemElement->exchangeRate;
$curr_code = (string) $itemElement->targetCurrency;
$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}

How to get the first element of a SimpleXML object?

Try

echo (string) $xml->Title;

You have to cast it as a string.



Related Topics



Leave a reply



Submit