When I try to convert XML(simpleXML) to JSON with json_encode, It works for XML without namesapce. For Example:
<ebpacket>
<head>
<packettype> UserAuthorization</packettype>
<staffcode> UserName </staffcode>
<pwd> Password </pwd>
<env> Evnironment </env>
</head>
</ebpacket>
When I convert XML like below with attributes, json_encode returns an empty json:
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/>
<soapenv:Header />
<soapenv:Body>
<ser:processTrans>
<xmlValue>
<ebpacket>
<head>
<packettype> UserAuthorization</packettype>
<staffcode> UserName </staffcode>
<pwd> Password </pwd>
<env> Evnironment </env>
</head>
</ebpacket>
</xmlValue>
</ser:processTrans>
The code block I am using is:
$xml_str = str_replace(PHP_EOL, '', $xmlstr);
$xml = simplexml_load_string($xml_str,'SimpleXMLElement',LIBXML_NOCDATA);
$json_object = json_encode($xml, JSON_PRETTY_PRINT);
TRUEinstead ofJSON_PRETTY_PRINTlike here: secure.php.net/manual/en/book.simplexml.php#105330 . But you also say that this is aSimpleXMLElementthat will have to be processed based on the namespace and that's why you have an empty string.