I've got a problem when I want to convert an XML string into a SimpleXMLElement object.
This is my PHP code:
// XML string
$xmlStr = <<<XML
<?xml version='1.0'?>
<document>
<lastname lang="EN">Smith</lastname>
</document>
XML;
// Convert the XML string into an Array
$xml = simplexml_load_string($xmlStr);
$json = json_encode($xml);
$xmlArray = json_decode($json, true);
// Print SimpleXMLElement object
print_r($xml);
echo '<br /><br />';
// Print JSON
print_r($json);
echo '<br /><br />';
// Print XML Array
print_r($xmlArray);
echo '<br /><br />';
And I get this result:
SimpleXMLElement Object ( [lastname] => Smith )
{"lastname":"Smith"}
Array ( [lastname] => Smith )
But there is no "lang" attribute and I don't know what I'm doing wrong... :(
Anyone can help me, please? Any idea would be very appreciated! Thanks in advance.
To set the text value 'Smith' of the 'lastname' tag I do:
$xml->lastname->{'_'}
Hope it helps!