2017-05-04 30 views
0

假设下面的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 
    } 
} 

有什么办法可以克服这个问题吗?

+0

显示,你就装XML内容 – RomanPerekhrest

+0

@BernardV希望我的文章会帮助你的代码... –

+0

@RomanPerekhrest我加载如下:'$ theXML = new SimpleXMLElement($ stringBody);' – BernardV

回答

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:SimpleXMLElementTry 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; 
+0

这是行不通的,非常感谢Sahil Gulati **但是**现在得到一个计数我必须使用DOMDocument来导入我的XML,你相信有什么办法可以这样做:SimpleXMLElement?谢谢! – BernardV

+0

谢谢@Sahil Gulati,我现在正在测试这个! – BernardV

+0

我会肯定的,谢谢你的协助! (已经投票了!) – BernardV