How do I iterate through DOM elements in PHP?

In PHP, you can iterate through DOM elements using the DOMDocument class along with methods like getElementsByTagName() and foreach loops to traverse child nodes.

Sample XML Data

Let's work with the following XML structure ?

<root>
    <foo>
        <bar>Value 1</bar>
        <pub>Data 1</pub>
    </foo>
    <foo>
        <bar>Value 2</bar>
        <pub>Data 2</pub>
    </foo>
    <foo>
        <bar>Value 3</bar>
        <pub>Data 3</pub>
    </foo>
</root>

Basic DOM Iteration

Here's how to iterate through specific elements and access their child nodes ?

<?php
$xmlString = '<root>
    <foo>
        <bar>Value 1</bar>
        <pub>Data 1</pub>
    </foo>
    <foo>
        <bar>Value 2</bar>
        <pub>Data 2</pub>
    </foo>
    <foo>
        <bar>Value 3</bar>
        <pub>Data 3</pub>
    </foo>
</root>';

$dom = new DOMDocument();
$dom->loadXML($xmlString);

// Get all 'foo' elements
$elements = $dom->getElementsByTagName('foo');
$data = array();

foreach($elements as $node) {
    foreach($node->childNodes as $child) {
        // Skip text nodes (whitespace)
        if($child->nodeType == XML_ELEMENT_NODE) {
            $data[] = array($child->nodeName => $child->nodeValue);
        }
    }
}

print_r($data);
?>
Array
(
    [0] => Array
        (
            [bar] => Value 1
        )

    [1] => Array
        (
            [pub] => Data 1
        )

    [2] => Array
        (
            [bar] => Value 2
        )

    [3] => Array
        (
            [pub] => Data 2
        )

    [4] => Array
        (
            [bar] => Value 3
        )

    [5] => Array
        (
            [pub] => Data 3
        )

)

Accessing Specific Child Elements

You can also access specific child elements by their tag names ?

<?php
$xmlString = '<root>
    <foo id="1">
        <bar>Value 1</bar>
        <pub>Data 1</pub>
    </foo>
    <foo id="2">
        <bar>Value 2</bar>
        <pub>Data 2</pub>
    </foo>
</root>';

$dom = new DOMDocument();
$dom->loadXML($xmlString);

$fooElements = $dom->getElementsByTagName('foo');

foreach($fooElements as $foo) {
    echo "Processing foo element:<br>";
    
    // Get specific child elements
    $barElements = $foo->getElementsByTagName('bar');
    $pubElements = $foo->getElementsByTagName('pub');
    
    if($barElements->length > 0) {
        echo "  bar: " . $barElements->item(0)->nodeValue . "<br>";
    }
    
    if($pubElements->length > 0) {
        echo "  pub: " . $pubElements->item(0)->nodeValue . "<br>";
    }
    
    echo "<br>";
}
?>
Processing foo element:
  bar: Value 1
  pub: Data 1

Processing foo element:
  bar: Value 2
  pub: Data 2

Key Points

  • Use getElementsByTagName() to get elements by tag name
  • Check nodeType to filter out text nodes (whitespace)
  • Access node values using nodeValue property
  • Use item(0) to get the first element from a NodeList

Conclusion

DOM iteration in PHP allows you to traverse XML/HTML documents systematically. Use getElementsByTagName() and foreach loops to access elements and their child nodes efficiently.

Updated on: 2026-03-15T08:52:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements