计算属于多个父项的XML文档中的子项总数

时间:2017-05-04 08:34:50

标签: php xml simplexml domdocument

假设以下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.我有什么方法可以克服这个问题吗?

1 个答案:

答案 0 :(得分: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: SimpleXMLElement Try 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;