<a>
  <b c="1"></b>
  <b c="1"></b>
  <b c="1"></b>
</a>上述XML simplexml如何获得 a 中有多少个b 节点 谢谢了

解决方案 »

  1.   

    $strXML = <<<TEST
    <a> 
      <b c="1"> </b> 
      <b c="1"> </b> 
      <b c="1"> </b> 
    </a>
    TEST;
    $so = simplexml_load_string($strXML);
    echo sizeof($so->b); 
      

  2.   

    Leizhoucn:
       如下代码:$count为所求。
       
    $XMLStr=<<<SimpleXMLStr
    <a>
      <b c="1"> </b>
      <b c="1"> </b>
      <b c="1"> </b>
    </a> 
    SimpleXMLStr;$xml = simplexml_load_string ($XMLStr);
    $result = $bar->xpath('a/b');
    $count=0;
    foreach($result as $node) {
       $count++;