Hello I'm having an issue parsing XML that I just can't seem to figure out. Here is a snippet of what I get when I dump the variable the contains the raw XML output:
<hcAmount currencyCode="USD" decimals="2">180</hcAmount>
However, when I run$xml = new \SimpleXMLElement($curlResponse);
I'm not getting the attributes (specifically currencyCode and decimals in this case. Here's a snippet of the hcAmount element:
["hcCharges"]=>
object(SimpleXMLElement)#452 (2) {
["hcDescription"]=>
string(9) "BASE RATE"
["hcAmount"]=>
string(3) "180"
}
It is missing the currencyCode and decimals attributes and I'm trying to sort out what I'm doing wrong to have those get dropped. Thank you!
This is a common confusion: the full content available in a SimpleXMLElement doesn't show up when using common debugging functions like print_r
or var_dump
, because of the way it's implemented.
If you follow the examples in the manual for how to access attributes, you will find that they are accessible:
$xml = '<example><hcAmount currencyCode="USD" decimals="2">180</hcAmount></example>';
$sx = new SimpleXMLElement($xml);
echo "The text content is " . (string)$sx->hcAmount
. " and the currencyCode atttribute is " . (string)$sx->hcAmount['currencyCode'];
Outputs The text content is 180 and the currencyCode atttribute is USD