Skip to content

Core\Xml\Node::children

$node->children(): array<Core\Xml\Node>

Returns the children of an XML node as an array, in the order of the document. A child is a node directly inside another node.

For the document node that Core\Xml::parse returns, the children are the root element and any comment or processing instruction beside it. For an element, the children are everything between its start tag and its end tag: elements, text, comments and processing instructions. A text, a comment and a processing instruction have no children, so for them the array is empty.

The spaces and line breaks between two elements are text nodes, so they are children too. Use kind() when you only want the elements.

The examples below visit every element of a document, show the text between elements, and read the lines of an order.

Return value

Returns array<Core\Xml\Node>. An array with one Core\Xml\Node per child. The spaces and line breaks between two elements are text nodes, and they are in the array too.

Examples

Visit every element

<?nvs
// Shows how to visit every element of a document, one level at a time.
class Tree
{
// This prints the name of each element. Each level is indented by two more spaces.
public static function show(Core\Xml\Node $node, uint $depth): void
{
foreach ($node->children() as Core\Xml\Node $child) {
if ($child->kind() == Core\Xml\Node::Kind::Element) {
echo Core\Str::repeat(' ', $depth), $child->name(), "\n";
Tree::show($child, $depth + 1);
}
}
}
}
Tree::show(Core\Xml::parse('<menu><file><open/><save/></file><help/></menu>'), 0);
Output
menu
file
open
save
help

The text between elements

<?nvs
// Shows that the spaces and line breaks between elements are children too.
string $xml = '<colors>
<color>red</color>
<color>blue</color>
</colors>';
Core\Xml\Node $document = Core\Xml::parse($xml);
foreach ($document->children() as Core\Xml\Node $root) {
// The root has three text nodes with line breaks and two elements. This prints 5.
echo Core\Arr::count($root->children()), "\n";
// `kind()` tells you which children are elements. This prints 2.
int $elements = 0;
foreach ($root->children() as Core\Xml\Node $child) {
if ($child->kind() == Core\Xml\Node::Kind::Element) {
$elements = $elements + 1;
}
}
echo $elements, "\n";
}
Output
5
2

Read the lines of an order

<?nvs
// Shows how to read the lines of an order that another system sent as XML.
string $order = '<order id="1042">
<line sku="A-100" qty="2">Coffee mug</line>
<line sku="B-200" qty="1">Tea towel</line>
</order>';
Core\Xml\Node $document = Core\Xml::parse($order);
foreach ($document->children() as Core\Xml\Node $root) {
int $lines = 0;
foreach ($root->children() as Core\Xml\Node $line) {
// The line breaks between the `line` elements are text nodes. This skips them.
if ($line->kind() != Core\Xml\Node::Kind::Element) {
continue;
}
// The product name is the one text node inside the `line` element.
// This prints 2 x Coffee mug, and then 1 x Tea towel.
foreach ($line->children() as Core\Xml\Node $name) {
echo $line->attributes()['qty'], ' x ', $name->text(), "\n";
}
$lines = $lines + 1;
}
echo 'lines: ', $lines, "\n";
}
Output
2 x Coffee mug
1 x Tea towel
lines: 2