XML to JSON conversion in php




$data = "<QRYRESULT>
<ISSUCCESS>Y</ISSUCCESS>
<EBLCUSTOMER ACCOUNTNO='11111'>
<CUSTACCTNO>121212</CUSTACCTNO>
<ACCTSTATUS>active</ACCTSTATUS>
<CCYDESC>BDT</CCYDESC>
<BALANCE>9999</BALANCE>
<AVAILABLEBALANCE>99</AVAILABLEBALANCE>
<CUSTOMERNAME>cus_name</CUSTOMERNAME>
<AMOUNTONHOLD>1000</AMOUNTONHOLD>
<ODLIMIT>99</ODLIMIT>
</EBLCUSTOMER>
</QRYRESULT>";


this is the XML string I am trying to convert. I have used the folloung code.



$result = str_replace(array("\n", "\r", "\t"), '', $data);
$xml = simplexml_load_string($result);
$object = new stdclass();
$object->webservice[] = $xml;
$result = json_encode($object);
header('content-Type: application/json');
echo $result;


And I am getting the following json data.



{
"webservice": [
{
"ISSUCCESS": "Y",
"CUSTSUMMARY": {
"@attributes": {
"ACCOUNT": "11111"
},
"IDACCOUNT": "1010101",
"CODACCTCURR": "BDT",
"NUMBALANCE": "99999",
"ACCTDESC": "22222",
"PRDNAME": "name"
}
}
]
}


But i don't want the "@attributes". I want the output like below:



{
"QRYRESULT": {
"ISSUCCESS": "Y",
"EBLCUSTOMER": {
"-ACCOUNTNO": "11111",
"CUSTACCTNO": "121212",
"ACCTSTATUS": "active",
"CCYDESC": "BDT",
"BALANCE": "9999",
"AVAILABLEBALANCE": "99",
"CUSTOMERNAME": "cus_name",
"AMOUNTONHOLD": "1000",
"ODLIMIT": "99"
}
}
}


How can I do that ??


No comments:

Post a Comment