当我尝试转换XML(simplexml的),以用JSON json_encode,它适用于XML没有namesapce。 例如:
<ebpacket>
<head>
<packettype> UserAuthorization</packettype>
<staffcode> UserName </staffcode>
<pwd> Password </pwd>
<env> Evnironment </env>
</head>
</ebpacket>
当我转换XML像下面的属性,json_encode返回一个空的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>
我使用的代码块:
$xml_str = str_replace(PHP_EOL, '', $xmlstr);
$xml = simplexml_load_string($xml_str,'SimpleXMLElement',LIBXML_NOCDATA);
$json_object = json_encode($xml, JSON_PRETTY_PRINT);