【发布时间】:2017-05-04 08:34:49
【问题描述】:
假设以下 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文档中有5 <Other>标签。
我已经能够统计<OtherDetails>标签的数量(答案是2)如下:
if (!empty($Record->PersonDetails->OtherRelatedDetails->OtherDetails)) {
foreach ($Record->PersonDetails->OtherRelatedDetails->OtherDetails as $NumberOfOtherDetails) {
$theNumberOfOthers = $NumberOfOtherDetails->count(); //returns 2
}
}
只要我尝试向下计数到<Other> 标签级别,它就会返回 0。我有什么办法可以克服这个问题吗?
【问题讨论】:
-
显示你加载xml内容的代码
-
@BernardV 希望我的帖子能帮到你...
-
@RomanPerekhrest 我加载如下:
$theXML = new SimpleXMLElement($stringBody);
标签: php xml simplexml domdocument