Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用SimpleXML计算xml元素/节点数_Php_Xml_Simplexml - Fatal编程技术网

Php 使用SimpleXML计算xml元素/节点数

Php 使用SimpleXML计算xml元素/节点数,php,xml,simplexml,Php,Xml,Simplexml,这可能是重复的,但到目前为止我没有发现任何有用的东西 我正在尝试使用simplexml计算XML文件的节点/子节点数 我试过这个 $xml = simplexml_load_file("./file.xml"); echo count($xml->rss->channel->item); 及 但只是输出0 下面是XML <?xml version="1.0"?> <rss xmlns:xsd="http://www.w3.org/2001/XMLSchema

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

我正在尝试使用
simplexml
计算
XML
文件的
节点/子节点数

我试过这个

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

但只是输出
0

下面是
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>

普德拉普德拉塞克特塞克特
http://www.trendyol.com//Pudra-Ceket-IW6140005046/UrunDetay/31066/9188683?
36普陀普陀塞克特岛w6140005046伊佩基尔塞克特岛
1423905
1423905_14
....

这应该适合您:

echo count($xml->channel->item);
编辑:

你为什么要这样做?因为表单
simplexml\u load\u file()
会得到如下结构(
print\r($xml);
):


由于这种结构,您必须像我上面所说的那样访问对象。

谢谢。当我尝试打印产品时,需要更多的帮助,如
print\r($xml->channel->item)。然后只打印其中没有
g:
的节点,例如描述、标题、链接等。如何休息?@MubinKhalid元素位于不同的xml名称空间中,如果您想访问它们,请参见此处:打印单个项目时,它不会拾取
1423905_14
echo count($xml->channel->item);
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [item] => Array
                (