2012-03-08 78 views
1

經過數小時的搜索,找到類似的線程,仍然無法使其工作我已經訴諸張貼我的具體問題。我想從使用SimpleXMLElement()的服務器獲取SOAP編碼的XML響應,但是我真的很難建立起工作的基本路徑。SimpleXMLElement() - XPath()和SOAP什麼都不返回

我已經試過兩種不同的方法:

的XPath():

public function XMLParseUserData($xml) 
{ 
    $ActivityData = new SimpleXMLElement($xml); 

    $ActivityData->registerXPathNamespace("ns", "http://webservices.website.net/"); 
    $basePath = $ActivityData->xpath('//ns:GetUserActivityDataResult/ActArray'); 

    foreach ($basePath->ACT as $userActivity) 
    { 
     $this->uGUID = $userActivity->UserGUID; 
     echo $this->uGUID."<br />"; 
    } 
} 

兒():

public function XMLParseUserData($xml) 
{ 
    $ActivityData = new SimpleXMLElement($xml); 

    $basePath = $ActivityData->children('soap',true)->Body->GetUserActivityDataResponse->GetUserActivityDataResult->ActArray->ACT; 

    foreach ($basePath as $userActivity) 
    { 
     $this->uGUID = $userActivity->UserGUID; 
     echo $this->uGUID."<br />"; 
    } 
} 

XML響應:

<?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <soap:Body> 
      <GetUserActivityDataResponse xmlns="http://webservices.website.net/"> 
       <GetUserActivityDataResult> 
        <ResponseCode>SUCCESS</ResponseCode> 
        <FailedUserCount>0</FailedUserCount> 
        <ActCount>1</ActCount> 
        <ActArray> 
         <ACT> 
          <UserGUID>0dc299ba-XXXX-XXXX-XXXX-7ca097d51eb6</UserGUID> 
          <ActDataCount>15</ActDataCount> 
          <ActData> 
           <ACT_DATA> 
            <Start>2012-03-05T08:40:00</Start> 
            <End>2012-03-05T09:00:00</End> 
            <SourceCount>1</SourceCount> 
            <SourceData> 
             <ACT_SRC_DATA> 
              <Source>ACTIPED</Source> 
              <TypeCount>3</TypeCount> 
              <TypeData> 
               <ACT_TYPE_DATA> 
                <Type>WALK</Type> 
                <S>40</S> 
                <C>2</C> 
                <D>20</D> 
                <T>16</T> 
               </ACT_TYPE_DATA> 
               <ACT_TYPE_DATA> 
                <Type>RUN</Type> 
                <S>20</S> 
                <C>2</C> 
                <D>20</D> 
                <T>10</T> 
               </ACT_TYPE_DATA> 
               <ACT_TYPE_DATA> 
                <Type>OTHER</Type> 
                <S>0</S> 
                <C>0</C> 
                <D>0</D> 
                <T>28</T> 
               </ACT_TYPE_DATA> 
              </TypeData> 
              <MetricCount>0</MetricCount> 
             </ACT_SRC_DATA> 
            </SourceData> 
           </ACT_DATA> 
           </ActData> 
          </ACT> 
         </ActArray> 
         <AsOfServerTimeGMT>2012-03-06T16:41:41.513</AsOfServerTimeGMT> 
        </GetUserActivityDataResult> 
       </GetUserActivityDataResponse> 
      </soap:Body> 
     </soap:Envelope> 

這兩種方法都可行,既讓我同樣的錯誤:

Warning: Invalid argument supplied for foreach() in /c08/domains/dev.mysite.com/html/class/XMLParse.class.php on line 29 
+0

[我得到很多* *更多的錯誤(http://codepad.org/該XML的z4tT7INz)。它是無效的,不能被解析爲XML。 – Gordon 2012-03-08 17:45:30

+1

我的不好。反應是巨大的+100多標籤,所以我縮短它的顯示目的和意外減少關閉標籤。發佈編輯。 – 2012-03-09 09:54:22

回答

0

它說:

<GetUserActivityDataResponse xmlns="http://webservices.website.net/"> 

和你正在嘗試做的

$basePath = $soapEnvelope 
    ->children('soap', true) 
    ->Body 
    ->GetUserActivityDataResponse 
    … 

這意味着你嘗試獲得明顯不存在的<soap:GetUserActivityDataResponse>。你所要做的(demo

$basePath = $soapEnvelope 
    ->children('soap', true) 
    ->Body 
    ->children('http://webservices.website.net/') 
    ->GetUserActivityDataResponse 
    ->GetUserActivityDataResult 
    ->ActArray 
    ->ACT; 

其實,你可以只是->children()跳回默認的命名空間,但我發現提供明確的命名空間更爲清楚。你的選擇。因爲你沒有指定命名空間ActArray

你的XPath失敗。另外,當xpath()成功時,它返回一個SimpleXmlElements數組。你試過array->ACT,這不起作用,因爲數組不是對象。第一個ActArray$basePath[0]。所以,你必須將代碼調整

$basePath = $soapEnvelope->xpath('//ns:GetUserActivityDataResult/ns:ActArray'); 
foreach ($basePath[0]->ACT as $userActivity) { 
… 

要直接獲得ACT元素,改變XPath來

//ns:GetUserActivityDataResult/ns:ActArray/ns:ACT 
+1

啊,我明白了。不遠處!非常好,非常感謝Gordon! – 2012-03-09 11:22:11