定義和用法
XML 是一種 mark-up 語言,用於在網絡上共享數據,XML 用於人類 read-able 和機器 read-able。 SimpleXMLElement 類表示 PHP 中的 XML 文檔。
這個SimpleXMLElement::count()函數計算 XML 元素的子元素數。
用法
SimpleXMLElement::count();
參數
該函數不接受任何參數。
返回值
此函數返回一個整數值,表示 XML 元素的子元素數。
PHP版本
這個函數最初是在 PHP 版本 5 中引入的,並且適用於所有後續版本。
示例
以下示例演示了 SimpleXMLElement::count() 函數的用法。
<html>
<head>
<body>
<?php
$str = "<?xml version='1.0' standalone='yes'?>
<Tutorial>
<Name type = 'programming'>JavaFX</Name>
<Pages>535</Pages>
<Author>Krishna</Author>
<Version>11</Version>
</Tutorial>";
$xml = new SimpleXMLElement($str);
print("Number of child elements:".$xml->count());
?>
</body>
</head>
</html>
這將產生以下結果 -
Number of child elements:4
示例
在以下示例中,我們嘗試讀取 XML 文件的內容,該程序打印 XML 文件的記錄數 -
data.xml:
<?xml version="1.0" encoding="utf-8"?> <Tutorials> <Tutorial> <Name>JavaFX</Name> <Pages>535</Pages> <Author>Krishna</Author> <Version>11</Version> </Tutorial> <Tutorial> <Name>CoffeeScript</Name> <Pages>235</Pages> <Author>Kasyap</Author> <Version>2.5.1</Version> </Tutorial> <Tutorial> <Name>OpenCV</Name> <Pages>150</Pages> <Author>Maruti</Author> <Version>3.0</Version> </Tutorial> </Tutorials>
sample.php:
<html> <head> <body> <?php $doc = new DOMDocument; $xml = simplexml_load_file("trail.xml"); //file to SimpleXMLElement $xml = simplexml_import_dom($xml); print("Number of child elements:".$xml->count()); ?> </body> </head> </html>
這將產生以下輸出 -
Number of child elements:3
相關用法
- PHP SimpleXMLElement::xpath()用法及代碼示例
- PHP SimpleXMLElement::getName()用法及代碼示例
- PHP SimpleXMLElement::registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement::getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement::getNameSpaces()用法及代碼示例
- PHP SimpleXMLElement::__toString()用法及代碼示例
- PHP SimpleXMLElement::__construct()用法及代碼示例
- PHP SimpleXMLElement::addChild()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- PHP SimpleXMLElement XPath()用法及代碼示例
- PHP SimpleXMLElement addAttribute()用法及代碼示例
- PHP SimpleXMLElement addChild()用法及代碼示例
- PHP SimpleXMLElement attributes()用法及代碼示例
- PHP SimpleXMLElement __toString()用法及代碼示例
- PHP SimpleXMLElement asXML()用法及代碼示例
- PHP SimpleXMLElement count()用法及代碼示例
- PHP SimpleXMLElement registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement getNamespaces()用法及代碼示例
- PHP SimpleXMLElement getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement saveXML()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP - SimpleXMLElement::count() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。