使用命名空间和属性迭代SOAP响应

时间:2015-05-22 18:38:27

标签: php web-services curl soap

我正在尝试解析从DHL Web Service获得的soap代码。我正在使用php cUrl生成请求,然后获得此响应(包括名称空间和属性):

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
  <rateresp:RateResponse xmlns:rateresp="http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgResponse">
     <Provider code="DHL">
        <Notification code="0">
           <Message/>
        </Notification>
        <Service type="C">
           <TotalNet>
              <Currency>EUR</Currency>
              <Amount>436.16</Amount>
           </TotalNet>
           <Charges>
              <Currency>EUR</Currency>
              <Charge>
                 <ChargeType>MEDICAL EXPRESS</ChargeType>
                 <ChargeAmount>392.94</ChargeAmount>
              </Charge>
              <Charge>
                 <ChargeType>FUEL SURCHARGE</ChargeType>
                 <ChargeAmount>43.22</ChargeAmount>
              </Charge>
           </Charges>
           <DeliveryTime>2015-05-19T09:00:00</DeliveryTime>
           <CutoffTime>2015-05-18T18:30:00</CutoffTime>
           <NextBusinessDayInd>N</NextBusinessDayInd>
        </Service>
        <Service type="K">
           <TotalNet>
              <Currency>EUR</Currency>
              <Amount>415.35</Amount>
           </TotalNet>
           <Charges>
              <Currency>EUR</Currency>
              <Charge>
                 <ChargeType>EXPRESS 9:00</ChargeType>
                 <ChargeAmount>374.19</ChargeAmount>
              </Charge>
              <Charge>
                 <ChargeType>FUEL SURCHARGE</ChargeType>
                 <ChargeAmount>41.16</ChargeAmount>
              </Charge>
              <Charge>
                 <ChargeType>9:00 PREMIUM</ChargeType>
                 <ChargeAmount>0.00</ChargeAmount>
              </Charge>
           </Charges>
           <DeliveryTime>2015-05-19T09:00:00</DeliveryTime>
           <CutoffTime>2015-05-18T18:30:00</CutoffTime>
           <NextBusinessDayInd>N</NextBusinessDayInd>
        </Service>
     </Provider>
  </rateresp:RateResponse>

现在我需要解析XML并将数据放入具有每个服务类型值的多维数组中。我坚持如何访问和迭代对象。这不起作用:

$element = new SimpleXMLElement($returned);
$element->registerXPathNamespace('r', 'http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgResponse');
$result = $element->xpath('//r:Service');
foreach ($result as $Service) {
  echo $Service . "\n";
}

0 个答案:

没有答案