【问题标题】:Count the total number of children in an XML document belonging to multiple parents计算属于多个父级的 XML 文档中的子级总数
【发布时间】: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 &lt;Other&gt;标签。

我已经能够统计&lt;OtherDetails&gt;标签的数量(答案是2)如下:

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

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

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

只要我尝试向下计数到&lt;Other&gt; 标签级别,它就会返回 0。我有什么办法可以克服这个问题吗?

【问题讨论】:

  • 显示你加载xml内容的代码
  • @BernardV 希望我的帖子能帮到你...
  • @RomanPerekhrest 我加载如下:$theXML = new SimpleXMLElement($stringBody);

标签: php xml simplexml domdocument


【解决方案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;

【讨论】:

  • 这确实有效,非常感谢 Sahil Gulati 但是现在我必须使用 DOMDocument 导入我的 XML,你相信有什么办法吗使用:SimpleXMLElement?谢谢!
  • 谢谢@Sahil Gulati,我现在正在测试这个!
  • 我一定会的,感谢您的帮助! (已经投票了!)
  • @BernardV 谢谢... :)
  • 解决方案 1 和解决方案 2 都很好用,我将使用解决方案 2,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多