user6344335Tejpal user6344335Tejpal - 3 months ago 9
JSON Question

How to extract xml response in php and create json response

after getting xml response from server using $result = curl_exec($ch);
i want to exctract some data from xml response and build that extracted data to valid json format and send that json to mobile app in android

this is what i am getting from my server.

<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<STATUS>1</STATUS>
</HEADER>
<BODY>
<DESC>
<CMPINFO>
...
</CMPINFO>
</DESC>
<DATA>
<COLLECTION ISMSTDEPTYPE="Yes" MSTDEPTYPE="512">
<STOCKITEM NAME="Abc Test" RESERVEDNAME="">
<STOCKITEMNAME TYPE="String">Abc Test</STOCKITEMNAME>
<CLOSINGBALANCE TYPE="Quantity"> 102 Nos</CLOSINGBALANCE>
<OPENINGBALANCE TYPE="Quantity"> 500 Nos</OPENINGBALANCE>
<DEBITTOTALS TYPE="Quantity"> 418 Nos</DEBITTOTALS>
<CREDITTOTALS TYPE="Quantity"> 20 Nos</CREDITTOTALS>
<OPENINGVALUE TYPE="Amount">-1000000.00</OPENINGVALUE>
<CLOSINGVALUE TYPE="Amount">-366400.00</CLOSINGVALUE>
<OUTWARDVALUE TYPE="Amount">4180000.00</OUTWARDVALUE>
<INWARDVALUE TYPE="Amount">-2400.00</INWARDVALUE>
<BASICQTY TYPE="Quantity"></BASICQTY>
<OPENINGRATE TYPE="Rate">2000.00/Nos</OPENINGRATE>
<CLOSINGRATE TYPE="Rate">1927.69/Nos</CLOSINGRATE>
<TBALCLOSING TYPE="Amount">-366400.00</TBALCLOSING>
<TBALOPENING TYPE="Amount">-1000000.00</TBALOPENING>
<STKOPBALANCE TYPE="Quantity"> 500 Nos</STKOPBALANCE>
<STKCLBALANCE TYPE="Quantity"> 102 Nos</STKCLBALANCE>
<TBALDEBITS TYPE="Amount">-2400.00</TBALDEBITS>
<TBALCREDITS TYPE="Amount">4180000.00</TBALCREDITS>
<STKINQTY TYPE="Quantity"> 20 Nos</STKINQTY>
<STKOUTQTY TYPE="Quantity"> 418 Nos</STKOUTQTY>
<TBALNETTCREDITS TYPE="Amount">4180000.00</TBALNETTCREDITS>
</STOCKITEM>
<STOCKITEM NAME="Gas" RESERVEDNAME="">
<STOCKITEMNAME TYPE="String">Gas</STOCKITEMNAME>
<CLOSINGBALANCE TYPE="Quantity"></CLOSINGBALANCE>
<OPENINGBALANCE TYPE="Quantity"></OPENINGBALANCE>
<DEBITTOTALS TYPE="Quantity"></DEBITTOTALS>
<CREDITTOTALS TYPE="Quantity"></CREDITTOTALS>
<OPENINGVALUE TYPE="Amount">0.00</OPENINGVALUE>
<CLOSINGVALUE TYPE="Amount"></CLOSINGVALUE>
<OUTWARDVALUE TYPE="Amount"></OUTWARDVALUE>
<INWARDVALUE TYPE="Amount"></INWARDVALUE>
<BASICQTY TYPE="Quantity"></BASICQTY>
<OPENINGRATE TYPE="Rate"></OPENINGRATE>
<CLOSINGRATE TYPE="Rate"></CLOSINGRATE>
<TBALCLOSING TYPE="Amount"></TBALCLOSING>
<TBALOPENING TYPE="Amount">0.00</TBALOPENING>
<STKOPBALANCE TYPE="Quantity"></STKOPBALANCE>
<STKCLBALANCE TYPE="Quantity"></STKCLBALANCE>
<TBALDEBITS TYPE="Amount"></TBALDEBITS>
<TBALCREDITS TYPE="Amount"></TBALCREDITS>
<STKINQTY TYPE="Quantity"></STKINQTY>
<STKOUTQTY TYPE="Quantity"></STKOUTQTY>
<TBALNETTCREDITS TYPE="Amount"></TBALNETTCREDITS>
</STOCKITEM>
....

</COLLECTION>
</DATA>
</BODY>
</ENVELOPE>


this is just only sample it gives very huge amt of data. suppose if i need only STOCKITEMNAME, CLOSINGBALANCE, STKINQTY etc ... tag values in json file. this values are in arrays. i tried like this...

<?php
.
.
.
$retValue = curl_exec($ch);
curl_close($ch);
$oXML = simplexml_load_string($retValue );
//echo $sXML;

$hd = array();

foreach ($oXML as $s):
$hd[$s]=$s->DATA->COLLECTION->STOCKITEM;
endforeach;

//$response["error"] = FALSE;
$response["resp"] = $hd;
//echo $header;
echo json_encode($response);

?>


but this is not working...
help me out please

Answer

STOCKITEM element is an array of elements. You have to crab each child element of STOCKITEM like STOCKITEMNAME etc.

<?php
$oXML = simplexml_load_string($retValue);

$i = 0;
$hd = [];
foreach ($oXML->BODY->DATA->COLLECTION->STOCKITEM as $item):
    $hd[$i]['STOCKITEMNAME']= (string) $item->STOCKITEMNAME;
    $hd[$i]['DEBITTOTALS']= (string) $item->DEBITTOTALS;
    # continue with elements you want ...
    $i++;
endforeach;
Comments