note 98443 modified in simplexmlelement.count by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Jun 2010 01:41:52 +0000 |
| Subject: | note 98443 modified in simplexmlelement.count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170162@lists.php.net to get a copy of this message | ||
I used the DOMNodeList $length property to simulate this function since my host had not yet
implemented PHP 5.3. The following code makes it portable and allows you to prepare for an upgrade
path.
<?php
$url = "http://www.example.com/foo.xml";
$xml = simplexml_load_file($url);
// For debugging
// echo "<p>".phpversion()."</p>";
if(phpversion() >= '5.3.0'){
$bar_count = $xml->foo->bar->count();
}
else {
$doc = new DOMDocument();
$str = $xml->asXML();
$doc->loadXML($str);
$bar_count = $doc->getElementsByTagName("bar")->length;
}
echo "There are ".$bar_count." /"bar/" nodes in this document.<br
/>";
?>
--was--
I used the DOMNodeList $length property to simulate this function since my host had not yet
implemented PHP 5.3. The following code makes it portable and allows you to prepare for an upgrade
path.
$url = "http://www.example.com/foo.xml";
$xml = simplexml_load_file($url);
// For debugging
// echo "<p>".phpversion()."</p>";
if(phpversion() >= '5.3.0'){
$bar_count = $xml->foo->bar->count();
}
else {
$doc = new DOMDocument();
$str = $xml->asXML();
$doc->loadXML($str);
$bar_count = $doc->getElementsByTagName("bar")->length;
}
echo "There are ".$bar_count." /"bar/" nodes in this document.<br
/>";
http://php.net/manual/en/simplexmlelement.count.php