Skip to content

SimpleXMLElement::children()

Introduction

SimpleXML is a PHP extension that provides a straightforward API for parsing and manipulating XML documents. Among its available methods, SimpleXMLElement::children() allows developers to retrieve child elements as SimpleXMLElement objects. This guide explains how to use the function effectively in PHP.

Understanding the SimpleXMLElement::children() function

The SimpleXMLElement::children() method returns an iterator of child elements for the current node. It accepts an optional $namespace parameter to filter results by a specific namespace. This method is essential for traversing XML hierarchies and handling namespaced documents.

Syntax

php
public function children(?string $namespace = null): SimpleXMLElement

This method returns a SimpleXMLElement object representing the child nodes. When $namespace is null (the default), it returns children from all namespaces. When a namespace URI string is provided, only child elements within that namespace are returned.

Note on default namespaces: If your XML uses a default namespace (no prefix), you must pass the namespace URI directly to children() to filter it. Passing null will return children from all namespaces, including those without a prefix.

Example Usage

Let's look at an example to understand how to traverse XML elements, filter by namespace, and handle mixed content in PHP:

php
<?php

$xml = simplexml_load_file('books.xml');
if ($xml === false) {
    die("Failed to load XML file.");
}

// Basic traversal
foreach ($xml->children() as $child) {
    foreach ($child->children() as $subChild) {
        echo $subChild->getName() . ": " . $subChild . "<br>";
    }
}

// Namespace filtering
$ns = $xml->getNamespaces(true);
foreach ($xml->children($ns['bk']) as $book) {
    echo $book->title . "<br>";
}

// Handling mixed content (text nodes alongside elements)
foreach ($xml->children() as $child) {
    echo $child->getName() . ": " . trim((string)$child) . "<br>";
}

In the example above, we first load an XML document from a file named books.xml using the simplexml_load_file() function. We add a basic check to handle cases where the file is missing or invalid. We use a foreach loop to iterate over each child element, and a nested loop to process sub-child elements. We use the children() method to retrieve the child objects and then print their name and value. The second block demonstrates namespace filtering by retrieving available namespaces and passing the target namespace to children(). The final block shows how to safely extract text content from elements that may contain mixed content by casting to a string and trimming whitespace.

Conclusion

The SimpleXMLElement::children() method provides a reliable way to access child elements within an XML structure. It is a valuable tool for developers working with XML data in PHP. By leveraging object-oriented traversal, namespace filtering, and proper text extraction, you can efficiently navigate and manipulate nested XML nodes. We hope this overview clarifies how to work with child elements in PHP SimpleXML. If you have any questions or need further assistance, please do not hesitate to ask.

Practice

What is the use of the getChildren() function in PHP?

Dual-run preview — compare with live Symfony routes.