使用$ result = curl_exec($ ch)从服务器获取xml响应后; 我想从xml响应中提取一些数据,并将提取的数据构建为有效的json格式,并将该json发送到android中的移动应用程序
这是我从服务器获取的内容。
<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>
这只是一个样本,它提供了非常庞大的数据。假设我只需要在json文件中使用STOCKITEMNAME,CLOSINGBALANCE,STKINQTY等标记值。这些值在数组中。我试过这样......
<?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);
?>
但这不起作用...... 请帮帮我
答案 0 :(得分:1)
STOCKITEM
元素是一个元素数组。你必须螃蟹STOCKITEMNAME
的每个子元素,如<?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;
等。
replace