BernardV
BernardV

Reputation: 766

Count the total number of children in an XML document belonging to multiple parents

Assuming the following 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>

I would like to do a count and find that there are 5 <Other> tags within the XML document.

I have been able to count the number of <OtherDetails> tags (answer being 2) as follows:

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

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

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

As soon as I try to go down and count to the <Other> tag level it returns 0. Is there any way for me to overcome this?

Upvotes: 0

Views: 37

Answers (1)

Sahil Gulati
Sahil Gulati

Reputation: 15141

Try this simplest one.

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);

Solution 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;

Upvotes: 1

Related Questions