count() function to get number of children of a nodeWe can get number of children present in XML element by using count() function. Here at different levels of node of the XML data we can get the number of children by using count function.
We will be using our sample page for this.
To make it more useful we will be using getName function to read about the element name along with the number of children present in that element.
The output is
To get the number of children at different nodes we will be using children function. Here is the complete code.
The output is here.
For PHP version less than 5.3 we have to use like this.
Use the xml file file-xml-demo.php
PHP & XML getName(): Name of the XML element or tag children(): Children of a XML node count(): Counting number of Children of an XML element simplexml_load_file(): creating object by reading from XML file simplexml_load_string(): creating object by using XML formatted string asXML: Getting string output or writing to file addAttribute(): Adding attribute to element of XML object addChild(): Adding Child element to element of XML object getNamespaces(): Getting NameSpaces from XML object XML from Database(): Generating XML from table records