children()
Learn how PHP's SimpleXMLElement::children() returns direct child elements of an XML node, with coverage of namespace filtering via $ns and $is_prefix.
Introduction
SimpleXMLElement::children() returns the direct child elements of an XML node as a new SimpleXMLElement. It is part of PHP's SimpleXML extension, the lightweight, object-oriented way to read and traverse XML. You reach for children() whenever you need to loop over or count the elements directly nested inside a node — instead of accessing them one property name at a time.
This page covers the method's signature and return value, how to iterate children, how to count them, and the part most tutorials skip: reading children that live in an XML namespace.
Syntax
public SimpleXMLElement::children(?string $namespace = null, bool $isPrefix = false): ?SimpleXMLElement| Parameter | Description |
|---|---|
$namespace | Optional. An XML namespace to filter children by. When omitted, children in the document's default namespace are returned. |
$isPrefix | Optional. If true, $namespace is treated as a namespace prefix (e.g. lib); if false (default), it is treated as a namespace URI (e.g. http://example.com/lib). |
Return value: a SimpleXMLElement you can iterate with foreach, index like an array ($children[0]), or pass to count(). It returns null only when called on something that is not a valid element.
children()returns direct children only — it does not descend into grandchildren. To reach deeper nodes, callchildren()again on a child, or use XPath.
Iterating over children
The most common use is looping over every direct child of a node:
Output:
PHP Basics - John Doe
Mastering XML - Jane RoeHere $xml is the <books> root, and children() yields each <book>. Inside the loop, $book->title and $book->author read the grandchild values. Because both <book> elements share the same tag name, looping is the natural way to handle a repeated structure.
Counting children
The returned object works with count(), so you can check how many direct children a node has, and index into them like an array:
<?php
$xml = new SimpleXMLElement('<menu><item>Tea</item><item>Coffee</item><item>Juice</item></menu>');
$children = $xml->children();
echo "Number of items: ", count($children), "\n";
echo "First item: ", $children[0], "\n";Output:
Number of items: 3
First item: TeaWorking with namespaces
This is where children() earns its parameters. When a document uses XML namespaces, a plain children() call only returns elements in the default namespace — prefixed elements are skipped. To reach them, pass the namespace.
<?php
$xml = new SimpleXMLElement(
'<library xmlns:lib="http://example.com/lib">
<lib:book>Namespaced Book</lib:book>
<note>Plain note</note>
</library>'
);
// No namespace: only children in the default namespace
echo count($xml->children()), " default-namespace child(ren)\n";
// Pass the namespace URI to reach the lib: children
$libChildren = $xml->children('http://example.com/lib');
echo count($libChildren), " lib child(ren): ", trim((string) $libChildren->book), "\n";
// Or pass the prefix with $isPrefix = true
$byPrefix = $xml->children('lib', true);
echo count($byPrefix), " child(ren) by prefix\n";Output:
1 default-namespace child(ren)
1 lib child(ren): Namespaced Book
1 child(ren) by prefixNotice the bare children() finds only <note>, while children('http://example.com/lib') finds the <lib:book> element. If you forget this, namespaced data appears to "vanish" — a classic SimpleXML gotcha.
Common pitfalls
- Only direct children are returned. Grandchildren require another
children()call or XPath. - Namespaced children need the namespace argument. A plain
children()silently ignores prefixed elements. - Values are objects, not strings. Cast a leaf node to a string (
(string) $child) or concatenate it before treating it as text. - Attributes are separate.
children()returns nested elements; useattributes()to read an element's attributes.
Conclusion
SimpleXMLElement::children() is the standard way to enumerate the direct child elements of an XML node in PHP, whether you are looping with foreach, counting with count(), or drilling into a specific namespace via the $namespace and $isPrefix parameters. Combine it with attributes() and simplexml_load_string() to parse real-world XML cleanly.