使用SimpleXML计数xml元素/节点


count xml elements/nodes using SimpleXML

这可能是重复的,但到目前为止我没有发现任何有用的东西。

我正在尝试使用simplexml计数XML文件的nodes/childs

我试过了。

$xml = simplexml_load_file("./file.xml");
echo count($xml->rss->channel->item);

echo count($xml->item);

而只是输出CCD_ 4。

这是XML 的快照

<?xml version="1.0"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:g="http://base.google.com/ns/1.0" version="2.0">
  <channel>
    <item>
      <title>PUDRA Pudra Ceket Ceket</title>
      <link>http://www.trendyol.com//Pudra-Ceket-IW6140005046/UrunDetay/31066/9188683?</link>
       <description>36 pudra pudra ceket ıw6140005046 ipekyol ceket kadın</description>
       <g:urunid>1423905</g:urunid>
       <g:id>1423905_14</g:id>
    </item>
    <item>
        ....
    </item>

这应该适用于您:

echo count($xml->channel->item);

编辑:

你为什么要这样做?因为从simplexml_load_file()可以得到这样的结构(print_r($xml);):

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )
    [channel] => SimpleXMLElement Object
        (
            [item] => Array
                (

由于这个结构,你必须访问对象,就像我说的(上面)。