2013年11月22日 星期五

解析轉換SOAP XML資料

XML範例:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <RestnoResponse xmlns="http://tempuri.org/">
      <RestnoResult>string</RestnoResult>
    </RestnoResponse>
  </soap12:Body>
</soap12:Envelope>

取出string的資料(php程式碼):


$xml = "XML範例";//也可將上範例資料存成檔案,再以file_get_contents()來取出資料
$soap = simplexml_load_string($xml);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
                 ->Body->children()
                 ->RestnoResponse
;
$string = (string) $response->RestnoResult;