假設下面的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
}
}
有什麼辦法可以克服這個問題嗎?
顯示,你就裝XML內容 – RomanPerekhrest
@BernardV希望我的文章會幫助你的代碼... –
@RomanPerekhrest我加載如下:'$ theXML = new SimpleXMLElement($ stringBody);' – BernardV