我有一个XML,我想转换为JSON。
XML如下。
<?xml version="1.0" encoding="UTF-8"?>
<ns0:COMMAND xmlns:ns0="http://www.tibco.com/schemas/pinless/PINLESS.core/C2STransferBillPayment/Schema.xsd9">
<ns0:TYPE>EXRCTRFRESP</ns0:TYPE>
<ns0:TXNSTATUS>7893</ns0:TXNSTATUS>
<ns0:DATE>14/02/18 12:22:30</ns0:DATE>
<ns0:EXTREFNUM>Dibon20180214122227pnqeP1yxqN</ns0:EXTREFNUM>
<ns0:TXNID />
<ns0:MESSAGE>No such user exists, password is invalid.</ns0:MESSAGE>
</ns0:COMMAND>
我试过了
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
但得到了simplexml_load_string(): Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content
。
有关如何将XML转换为JSON的任何建议吗?
我认为你有一个命名空间问题
$responsexml = simplexml_load_string($xml_string);
$child_data_response = $responsexml->children('http://www.tibco.com/schemas/pinless/PINLESS.core/C2STransferBillPayment/Schema.xsd9');
print_r($child_data_response);
从那里你可以使它成为一个对象,你可以json编码或做任何你想要的东西