2017-05-04 51 views
0

假設下面的XML:計數XML文檔中的兒童總數屬於多個家長

<Record> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC>    
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
    <OtherRelatedDetails> 
     <OtherDetails> 
      <Other> 
      <core:AuthNum>3761626</core:AuthNum>      
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>A</core:AuthType> 
      <core:Name>ABC</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>4383908</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>B</core:AuthType> 
      <core:Name>DEF</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>8103583</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>C</core:AuthType> 
      <core:Name>GHI</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
     </OtherDetails> 
    </OtherRelatedDetails> 
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC>       
</PersonDetails> 
    <PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
    <OtherRelatedDetails> 
     <OtherDetails> 
      <Other> 
      <core:AuthNum>9698550</core:AuthNum>      
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>A</core:AuthType> 
      <core:Name>ABC</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>8483953</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>B</core:AuthType> 
      <core:Name>DEF</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other>   
     </OtherDetails> 
    </OtherRelatedDetails> 
</PersonDetails> 
</Record> 

我願做一個數,發現有在XML文檔中 <Other>標籤。

我已經能夠算的<OtherDetails>標籤數(答案是)如下:當我嘗試下去,數到返回0 <Other>標籤水平

if (!empty($Record->PersonDetails->OtherRelatedDetails->OtherDetails)) { 

    foreach ($Record->PersonDetails->OtherRelatedDetails->OtherDetails as $NumberOfOtherDetails) { 

     $theNumberOfOthers = $NumberOfOtherDetails->count(); //returns 2 
    } 
} 

有什麼辦法可以克服這個問題嗎?

+0

顯示,你就裝XML內容 – RomanPerekhrest

+0

@BernardV希望我的文章會幫助你的代碼... –

+0

@RomanPerekhrest我加載如下:'$ theXML = new SimpleXMLElement($ stringBody);' – BernardV

回答

1

試試這個最簡單的。

Try this code snippet here

<?php 

ini_set('display_errors', 1); 
libxml_use_internal_errors(true); 
$domObj= new DOMDocument(); 
$domObj->loadXML('<Record> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC>    
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
    <OtherRelatedDetails> 
     <OtherDetails> 
      <Other> 
      <core:AuthNum>3761626</core:AuthNum>      
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>A</core:AuthType> 
      <core:Name>ABC</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>4383908</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>B</core:AuthType> 
      <core:Name>DEF</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>8103583</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>C</core:AuthType> 
      <core:Name>GHI</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
     </OtherDetails> 
    </OtherRelatedDetails> 
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC>       
</PersonDetails> 
    <PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
    <OtherRelatedDetails> 
     <OtherDetails> 
      <Other> 
      <core:AuthNum>9698550</core:AuthNum>      
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>A</core:AuthType> 
      <core:Name>ABC</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>8483953</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>B</core:AuthType> 
      <core:Name>DEF</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other>   
     </OtherDetails> 
    </OtherRelatedDetails> 
</PersonDetails> 
</Record>'); 
print_r($domObj->getElementsByTagName("Other")->length); 

解決方案2:SimpleXMLElementTry this code snippet here

<?php 


ini_set('display_errors', 1); 
libxml_use_internal_errors(true); 
$domObj= new SimpleXMLElement('<Record> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC>    
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
    <OtherRelatedDetails> 
     <OtherDetails> 
      <Other> 
      <core:AuthNum>3761626</core:AuthNum>      
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>A</core:AuthType> 
      <core:Name>ABC</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>4383908</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>B</core:AuthType> 
      <core:Name>DEF</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>8103583</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>C</core:AuthType> 
      <core:Name>GHI</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
     </OtherDetails> 
    </OtherRelatedDetails> 
</PersonDetails> 
<PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC>       
</PersonDetails> 
    <PersonDetails> 
    <StuffA>050656770</StuffA> 
    <StuffB>Stuff B Content</StuffB> 
    <StuffC>Stuff C Content</StuffC> 
    <OtherRelatedDetails> 
     <OtherDetails> 
      <Other> 
      <core:AuthNum>9698550</core:AuthNum>      
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>A</core:AuthType> 
      <core:Name>ABC</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other> 
      <Other> 
      <core:AuthNum>8483953</core:AuthNum> 
      <core:FromDate>2007-11-22</core:FromDate> 
      <core:ToDate>9999-12-31</core:ToDate> 
      <core:AuthType>B</core:AuthType> 
      <core:Name>DEF</core:Name> 
      <core:Num>3205355</core:Num> 
      </Other>   
     </OtherDetails> 
    </OtherRelatedDetails> 
</PersonDetails> 
</Record>'); 
$count=0; 
foreach($domObj->PersonDetails as $key => $object) 
{ 
    if(isset($object->OtherRelatedDetails->OtherDetails->Other)) 
    { 
     $count+=count($object->OtherRelatedDetails->OtherDetails->Other); 
    } 
} 
echo $count; 
+0

這是行不通的,非常感謝Sahil Gulati **但是**現在得到一個計數我必須使用DOMDocument來導入我的XML,你相信有什麼辦法可以這樣做:SimpleXMLElement?謝謝! – BernardV

+0

謝謝@Sahil Gulati,我現在正在測試這個! – BernardV

+0

我會肯定的,謝謝你的協助! (已經投票了!) – BernardV